user3386779
user3386779

Reputation: 7185

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

 <?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 
        }
  ?>

Upvotes: 1

Views: 5265

Answers (3)

user2663434
user2663434

Reputation:

Just append i and increment it till your while loop goes on..

<?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>

Upvotes: 2

underscore
underscore

Reputation: 6887

Don't use foreach as bellow way.

foreach($sql1 as $value){

// don't generate html here

}

Because it may brake the html

Use foreach bellow way.

<?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() ?>

Upvotes: 1

user3173819
user3173819

Reputation: 62

use the uniqid() function $str=uniqid("uid") it will make some thing like ti uid1hq7266
and print it in the input

Upvotes: 0

Related Questions