在while循环中创建唯一的表单输入ID和输入名称 [英] Create unique Form input ID's and input name's in while loop

查看:239
本文介绍了在while循环中创建唯一的表单输入ID和输入名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表正在建立和填充一个while循环。 while循环提取并显示来自mysql数据库的结果。

根据以下代码,每行都有一个复选框,其值等于唯一标识符 loadnumber



下一列是名称和编号为的权重。再次,这个值是由MySQL数据值预定义的。下一列是一个名为 correctedtrailer 的选择框,选中的值又等于数据库结果。



我的SQL查询结果有很多行,所以我的表是几行深。我想要做的是允许用户选中复选框复选框[] ,更改修改的权重 corretrailer 放在同一行,然后提交表单。问题是,如果我有多行,所有我的校正权 correctedtrailer 的名字都是一样的,所以我不能区分他们。

 < while($ row = $ loads-> fetch(PDO :: FETCH_ASSOC)){?> 
< tr>
< td>< input type =checkboxname =checkbox []id =checkbox []value =< ;? echo $ row ['loadnumber'];?>> ;< / TD>
< td>< input type =textname =correctedweightid =correctedweightvalue =<?echo $ row ['weight'];?>类= inputsmall >< / TD>
< td>< SELECT name =correctedtrailerid =correctedtrailerclass =selectnarrow>

为了解决这个问题,我可以将loadnumber追加到每个输入字段的名称和ID吗? p>

< input type =textname =correctedweight <?echo $ row ['loadnumber'];?> id =校正权重?echo $ row ['loadnumber'];?> value =< ;? echo $ row ['weight'];?> class =inputsmall>



然后,这将为我的表单输入一个唯一的名称。接下来的问题是如何为选定的行调用这个名字?在$ 复选框[] 数组中将包含加载数,所以在我的 foreach($ _ POST ['checkbox']为$ checkbox){语句,我如何获得修正的权重值<? echo $ row ['loadnumber']; ?> ?它是$ _POST ['校正重量。$复选框']或如何工作?

此外这是这样做的最好方法吗? b

一如既往地受到赞赏。谢谢

我的表单,输入和选择字段预填充MySQL数据的图像。希望能够同时检查和编辑多个表行,但是需要一个唯一的标识符作为使用构建的表,而循环。再次感谢。

解决方案

我可以看到你正在循环访问数据库条目。我会建议使用你的负载号码作为标识符在所有的领域,然后循环通过它在后,你可以追加它,就像你以上所做的那样。

类似这样:

 < td>< input type =checkboxname =复选框[< ;? echo $ row ['loadnumber '];?>]id =checkbox [< ;? echo $ row ['loadnumber'];?>]value =< ;? echo $ row ['loadnumber'];?>> ;< / TD> 
< td>< input type =textname =correctedweight_<?echo $ row ['loadnumber'];?> id =correctedweight_< ;? echo $ row ['loadnumber'];?> value =< ;? echo $ row ['weight'];?>类= inputsmall >< / TD>
< td>< SELECT name =correctedtrailer_< ;? echo $ row ['loadnumber'];?> id =correctedtrailer_< ;? echo $ row ['loadnumber'];?>类= selectnarrow >
< option value =< ;? echo $ row ['trailer'];?>选择><? echo $ row ['trailer']; ?>< /选项>
< option><?= $ optionstrailer?>< / option>
< / SELECT>

获取每篇复选框的信息:

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

$ / code>

请记住,如果我记得正确,只会得到复选框:)



希望这会有所帮助,祝你好运

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.

解决方案

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!

这篇关于在while循环中创建唯一的表单输入ID和输入名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆