Skip to content
Advertisement

updating multiple records in php from multiple textbox (textbox name depends on number of records)

I have a table that holds records of grades of student, these students are in a group. some have 5 members some have 4, 3 or 2. The page where grades can be edited returns all the names of the group selected with a textbox corresponding to each of them.

this is the part of my code that returns the said output:

<table>
while($data= mysql_fetch_array($query))
{
echo '<tr>
<td>'.$data['Student Name'].'</td>
<td><input type="text" name="HELP_ME_HERE" value="" /></td>
</tr>';
}
</table>

let’s say the $query returned 3 records. what I need to happen is that the 3 textboxes that would be echoed out should have different Name like for example it should be named as HELP_ME_HERE1, HELP_ME_HERE2, HELP_ME_HERE3. I need that to happen so I could easily call each values to be used for updating all 3 records in 1 click. Thanks in advance

Advertisement

Answer

you can do this like:

<script type="text/javascript">
var val;
function fun(val)
{
  if(val<50 || val>100)
  {
     alert("You are allowed to enter values 50-100");
     document.getElementById("num").value="";
  }
}    
</script>


     <table>
        <?php 
        $i=1;
        while($data= mysql_fetch_array($query))
        {  ?>
        <tr>
        <td> <?php echo $data['Student Name']; ?></td>
        <td><input type="text" name="HELP_ME_HERE<?php echo $i; ?>" value="" onkeydown="return (event.keyCode>=48 && event.keyCode<=57)|| event.keyCode==8" onchange="fun(this.value)" /></td>
        </tr>
       <?php $i++; }  ?>
      </table>

Here i have simply added php code in html and a variable is incremented which will make your name as in increment form.

  1. In this editing, you can see on change of the value i.e. less than 50 or more than 100, it will give alert box and erase the textbox value. And you can see in onkeydown i have restricted users to press only number keys or backspace if required.

Hope this solves your query.

Thanks and Regards.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement