Create unique Form input ID's and input name&#

2019-08-30 11:44发布

I have a table being built and populated by a while loop. the while loop is fetching and displaying results from a mysql database.

As per the below code, there is a checkbox on each line, value equal to a unique identifier loadnumber.

the next column is a input box with name and id of correctedweight. again the value of this is predefined by the MySQL data value. the next column is a select box named correctedtrailer and again the selected value is equal to the database result.

My sql query result has many lines in so my table is several lines deep. What I am wanting to do, is allow the user to check the checkbox checkbox[], change the values of correctedweight and correctedtrailer on the same row and then submit the form. the problem is that if I have several rows, the names of all my correctedweight and correctedtrailer are all the same so I cant differentiate between them.

<? while($row = $loads->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>        
    <td><input type="checkbox" name="checkbox[]" id="checkbox[]" value="<? echo $row['loadnumber']; ?>"></td>
    <td><input type="text" name="correctedweight" id="correctedweight" value="<? echo $row['weight']; ?>" class="inputsmall"></td>
    <td><SELECT name="correctedtrailer" id="correctedtrailer"  class="selectnarrow"> 
    <OPTION value="<? echo $row['trailer']; ?>" selected ><? echo $row['trailer']; ?></option>
    <option>
    <?=$optionstrailer?> 
    </option>
    </SELECT> 
    </td>
</tr>

to fix this, can I append the loadnumber to the name and id of each input field?

<input type="text" name="correctedweight<? echo $row['loadnumber']; ?>" id="correctedweight<? echo $row['loadnumber']; ?>" value="<? echo $row['weight']; ?>" class="inputsmall">

This will then give each input of my form a unique name. next question is how do I call this name for the selected rows? the checkbox[] array will contain the load number so in my foreach($_POST['checkbox'] as $checkbox){ statement, how do I get the value of correctedweight<? echo $row['loadnumber']; ?>? is it $_POST['correctedweight.$checkbox'] or how does it work?

additionally is this the best way of doing this?

help appreciated as always. Thanks

image of my form, input and select fields prepopulated with MySQL data. want the ability to check and edit several of the table rows at once but need a unique identifier as table built with while loop. thanks again. enter image description here

1条回答
forever°为你锁心
2楼-- · 2019-08-30 12:12

from what I can see you are looping through database entries. I would suggest using your load number as an identifier in all your fields, then looping through it in the post you can append it like you did above.

Something like this:

<td><input type="checkbox" name="checkbox[<? echo $row['loadnumber']; ?>]" id="checkbox[<? echo $row['loadnumber']; ?>]" value="<? echo $row['loadnumber']; ?>"></td>
<td><input type="text" name="correctedweight_<? echo $row['loadnumber']; ?>" id="correctedweight_<? echo $row['loadnumber']; ?>" value="<? echo $row['weight']; ?>" class="inputsmall"></td>
<td><SELECT name="correctedtrailer_<? echo $row['loadnumber']; ?>" id="correctedtrailer_<? echo $row['loadnumber']; ?>"  class="selectnarrow"> 
    <option value="<? echo $row['trailer']; ?>" selected ><? echo $row['trailer']; ?></option>
    <option><?=$optionstrailer?></option>
</SELECT> 

Getting the information per checkbox in the post:

foreach($_POST['checkbox'] as $checkbox){
    $correctedweight = $_POST['correctedweight_$checkbox'];
    $correctedtrailer = $_POST['correctedtrailer_$checkbox'];
}

Remember you will only get checkboxes that was checked if I remember correct :)

Hope this helps, good luck!

查看更多
登录 后发表回答