I am having trouble getting the values of the unknown texts inputs. I currently have a for loop that will iterate through an array of rows returned by mysql query. The data returned is iterated and displayed on the page. Each row contains its own form and buttons. If the button is clicked, I would like for it to grab the values that were inside of that same row in the array and send them to another page. My question would be, since the number of rows is unknown, how would I be able to get just the values inside of the rows for which the button was clicked on?
Edit:
As you can see in the image, the accordion and its cards inside are generated by the for the loop. The number of cards will depend on the number of rows returned by the MySQL query. To verify my issue, each "card" has inputs inside where the values also come from the MySQL query, therefore, if the user clicks on "edit client", I would only like to grab the values of the textbox inside of its card. Below is the code that generates each card:
Code:
$counter = 0;
foreach($clients as $client) {
echo '
<div class="card">
<div class="card-header" role="tab" id="heading'. $counter .'">
<h5 class="mb-0">
<div class="row">
<div class="d-flex align-items-center schedule-flex">
<div class="col-8 col-md-10">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse'. $counter .'" aria-expanded="true" aria-controls="collapse'. $counter .'">
'. $client['fName'] .' '. $client['lName'] .'
</a>
</div>
</div>
</div>
</h5>
</div>
<div id="collapse'. $counter .'" class="collapse" role="tabpanel" aria-labelledby="heading'. $counter .'">
<div class="card-block">
<form action="editClient.php">
<div class="row">
<div class="col-12 col-md-6">
<label for="fname">First Name</label>
<input name="fname" class="form-control" type="text" value="'. $client['fName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="lname">Last Name</label>
<input name="lname" class="form-control" type="text" value="'. $client['lName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="address">Address</label>
<input name="address" class="form-control" type="text" value="'. $client['address'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="phone">Phone Number</label>
<input name="phone" class="form-control" type="text" value="'. $client['telephone'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="email">Email Address</label>
<input name="email" class="form-control" type="text" value="'. $client['email'] .'" disabled>
</div>
</div>
<button class="btn btn-lg btn-primary btn-block schedule-submit" type="submit">Edit Client</button>
<br />
</form>
</div>
</div>
</div>
';
$counter++;
As far I can see I think you have the problem submitting the form. And as per your description and code, it looks like you are generating multiple forms.
method = "post"
so I have added the action.Here is the code:
$counter = 0;
foreach($clients as $client) {
echo '
<div class="card">
<div class="card-header" role="tab" id="heading'. $counter .'">
<h5 class="mb-0">
<div class="row">
<div class="d-flex align-items-center schedule-flex">
<div class="col-8 col-md-10">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse'. $counter .'" aria-expanded="true" aria-controls="collapse'. $counter .'">
'. $client['fName'] .' '. $client['lName'] .'
</a>
</div>
</div>
</div>
</h5>
</div>
<div id="collapse'. $counter .'" class="collapse" role="tabpanel" aria-labelledby="heading'. $counter .'">
<div class="card-block">
<form action="editClient.php" method="post" id="form'. $counter .'">
<div class="row">
<div class="col-12 col-md-6">
<label for="fname">First Name</label>
<input name="fname" class="form-control" type="text" id="fname'. $counter .'" value="'. $client['fName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="lname">Last Name</label>
<input name="lname" class="form-control" type="text" id="lname'. $counter .'" value="'. $client['lName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="address">Address</label>
<input name="address" class="form-control" type="text" id="address'. $counter .'" value="'. $client['address'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="phone">Phone Number</label>
<input name="phone" class="form-control" type="text" id="phone'. $counter .'" value="'. $client['telephone'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="email">Email Address</label>
<input name="email" class="form-control" type="text" id="email'. $counter .'" value="'. $client['email'] .'" disabled>
</div>
</div>
<button class="btn btn-lg btn-primary btn-block schedule-submit edit_client_button" id="'. $counter .'" type="submit">Edit Client</button>
<br />
</form>
</div>
</div>
</div>
';
$counter++;
// Saperatedly add the Script to with your javascript files. I have added here just for example. put it in a right place.
<script type="text/javascript">
$(".edit_client_button").click(function() {
var buttonid = this.id;
$( "#form"+buttonid ).submit();
});
</script>
I have added the id
attribute in each input from which you want to get value on click. Updated Script for getting input value on button click.
<script type="text/javascript">
$(".edit_client_button").click(function() {
var buttonid = this.id;
var fname = $('#fname'+buttonid); //Kevin
var lname = $('#lname'+buttonid); //Smith
var address = $('#address'+buttonid); //1212 Test Street
var phone = $('#phone'+buttonid); //8472934569
var email = $('#email'+buttonid); //kevin.smith@gmail.com
// This variabls contail the input value which you want on click.
// Use console.log(); to see the value or simply alert();
// example: console.log(email); or alert(email);
});
</script>
As per your comment i didn't get you properly. You mention that you you need a value form input. example:
If the button, "edit client" inside Kevin Smith is clicked, I would like for my php script to grab the values inside the textbox, i.e. Kevin, Smith, 1212 Test Street, 2929292929 and test.test@gmail.com.
My suggested script is gives you all the value within the form, however if you want to get that input value separately they i have update the answer code please check that.