PHP: How to get the value of an unknown textbox?

I am having problems getting the values ​​of unknown texts. I currently have a for loop that will loop through an array of strings returned by a mysql query. The returned data is repeated and displayed on the page. Each line contains its own form and buttons. If the button is clicked, I would like it to grab the values ​​that were inside the same string in the array and send them to another page. My question would be, since the number of rows is unknown, how could I only get the values ​​inside the rows for which the button is clicked?

Edit:

enter image description here

As you can see in the image, the accordion and its maps inside are generated per cycle. The number of cards will depend on the number of rows returned by the MySQL query. To test my problem, each "map" has inputs inside where the values ​​also come from a MySQL query, so if the user clicks on "change client" I would only like to grab the textbox values ​​inside their map. 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++;

      

+3


source to share


1 answer


As far as I can see, I think you are having a problem submitting the form. And as per your description and code, it looks like you are creating multiple forms.

  • Not in your form method = "post"

    , so I added an action.
  • To submit each form separately, I added an ID that is unique to each form using a spinner.
  • Added a button ID that contains a spinner number, so each form has a unique form ID and a button ID, so you can now submit this form separately.
  • You can now submit the form using PHP.
  • I added a script to submit the form. which first grab the button id and then using that button id it will submit the form.
  • And finally, check if validation is correct before submitting if you like.

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 an attribute id

to each input you want to get the value from on click. Updated script to get 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 right. You mentioned that you need to enter a value form. Example:

If I click the "change client" button inside Kevin Smith, I would for example 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 gives you all the value in the form, however, if you want to get this input value separately, I have an updated answer code, please check this.

0


source







All Articles