[英]in php how can i generate dynamic id for each input textbox created in while loop
how to get the selected textbox id generated in while loop for updating the field as in phpmyadmin 如何获取在while循环中生成的选定文本框ID以更新字段,如phpmyadmin中所示
<?php
while($fet=mysql_fetch_assoc($sql1))
{
?>
<tr>
<td><input type=text id='hello' name='hello' data-id="<?php echo $fet['username']?>" onchange='loadXMLDoc(this);' value=<?php echo $fet['username']?>>
<td><input type=text id='uid' name='uid' onchange='loadXMLDoc(this).' value=<?php echo $fet['id']?>>
</tr>
<?php
}
?>
Just append i and increment it till your while loop goes on.. 只需追加我并递增它直到你的while循环继续..
<?php
$i=1;
while($fet=mysql_fetch_assoc($sql1))
{
echo '
<tr>
<td>
<input type="text" id="hello'.$i.'" name="hello'.$i.'" data-id="'.$fet['username'].'" onchange="loadXMLDoc(this.value,'.$i.')" value='.$fet['username'].' >
</td>
</tr>';
here write ur code for other fields
$i++;
}
?>
<script>
function loadXMLDoc(a,i)
{ var d= document.getElementById("hello"+i).value; }
</script>
Don't use foreach as bellow way. 不要像使用foreach那样使用foreach。
foreach($sql1 as $value){
// don't generate html here
}
Because it may brake the html 因为它可能会制动html
Use foreach bellow way. 使用foreach方式。
<?php
foreach($sql1 as $value):
?>
<tr>
<td><input type=text id='hello' name='hello' data-id="<?php echo $value['username']?>" onchange='loadXMLDoc(this);' value=<?php echo $value['username']?>>
<td><input type=text id='uid' name='uid' onchange='loadXMLDoc(this).' value=<?php echo $value['id']?>>
</tr>
<?php endforeach() ?>
use the uniqid()
function $str=uniqid("uid")
it will make some thing like ti uid1hq7266 使用
uniqid()
函数$str=uniqid("uid")
它会产生一些像ti uid1hq7266
and print it in the input 并在输入中打印
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.