简体   繁体   English

在PHP中如何为while循环中创建的每个输入文本框生成动态ID

[英]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.

相关问题 如何获取动态创建的文本框的动态输入文本ID - how to fetch dynamic input text id of dynamically created textbox 如何在每个for循环的输入字段中加1的id? - How can I add id by 1 for input field in each for loop? 如何使用php将动态文本框值插入相同ID的数据库表中? - How can i insert dynamic textbox values into database table on same ID using php? 如何获取在cshtml中使用循环创建的每个div / li / a的特定元素的Id - How can I get the Id of the particular element of each div / li / a that is created using loop in cshtml 如何获取输入类型的动态ID? - How can I get the id the dynamic ID of input type? 如何在等待用户输入时暂停jQuery .each()循环? - How can I pause a jQuery .each() loop, while waiting for user input? 如何为动态创建的文本框输入生成不同的名称 - How to generate different name for textbox input which is created dynamically 我如何在while循环中添加输入id值(基于迭代),该while循环引用其中的一段html? - How can I add an input id value (iteration-based) in a while loop that references a piece of html outside of it? 在while循环中创建的文本框的值 - the value of the textbox created in while loop 如何在jQuery中获取动态创建的文本框的动态ID - how to get dynamic id of dynamically created textbox in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM