Patrick John Calumno
Patrick John Calumno

Reputation: 89

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

Upvotes: 1

Views: 1272

Answers (2)

Abbas
Abbas

Reputation: 412

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.

Upvotes: 1

crafter
crafter

Reputation: 6296

You can use arrays in forms

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

This will then pass the values of the field name as an array. You can parse it in PHP as follows :

   foreach ($_POST['StudentName'] as $studentInput) {
       echo 'Student name supplied is ' . $studentInput . "\n";
   }

Note that you can even supply an index in the html form, Assume you want to use the student id form the database.

<?php while($data= mysql_fetch_array($query)) {  ?>
    <tr>
        <td><input type="text" name="StudentName[<?php echo $data['Student Id']; ?>]" value="<?php echo $data['Student Name']; ?>" /></td>
    </tr>

Your PHP code will then change to :

   foreach ($_POST['StudentName'] as $studentId => $studentInput) {
       echo 'Student name for id ' . $studentId . 'is ' . $studentInput . "\n";
   }

Upvotes: 0

Related Questions