David Mukoro
David Mukoro

Reputation: 467

Checkbox with Result from Database in PHP

I have a record fetch from database based on a dropdown selection.The result is shown as a table, in which the user is expected to tick his choice.After the selection,the user clicks a button which will automatically add his selection to a table.Now the issue is: the id that is fetched for each item changes when the button is clicked by turning it to ascending number.see image below:enter image description here

For instance, in the table below, id 773,774,777,895 and 901 are selected.When Add to Float Button is cliked, the Id now becomes: 773,774,775,776,777(arranged ascending). See code below: for Displaying Table

<table>
<tr>
<td><input type="text" value="<?php echo $r['item_code'];?>" name="itmcode[]"  readonly="readonly"/></td>
    <td><?php echo $r['description'];?></td>
    <td><?php echo $r['qty'];?></td>
    <td><?php echo $r['price_per_qty'];?>
    <td><input type="text" value="<?php echo $r['total_value'];?>" name="tvalue[]" readonly="readonly" /></td>
    <td><?php echo $r['preferred_supplier'];?></td>
   <td><input type="checkbox" name="chkbx[]"  value="<?php echo $r['id'];?>">
   <input type="hidden" name="gid[]"  value="<?php echo $r['id'];?>">
</tr>
</table>

Processing Script:

<?php
if(array_key_exists('chkbx', $_POST)&&(!empty($_POST['chkbx']))&&(isset($_POST['floatBtn']))){
    foreach($_POST['chkbx'] as $rec=>$value)
      {
        $itm = $_POST['itmcode'][$rec];
        $tval = $_POST['tvalue'][$rec];
        $t = $_POST['gid'][$rec];
        $apno =$_POST['aNo']; 
        $fno = $_POST['fno'];  
        echo "itm:".$itm." tval: ".$tval." t:".$t." appno:".$apno."fno:".$fno."<br/>";
      }
    }
?>

How can I correct this, so that it can display the correct id when the button is clicked after selecting.

Upvotes: 1

Views: 60

Answers (1)

Arun Krish
Arun Krish

Reputation: 2153

Change your code like this

<table>
  <tr>
 <td><input type="text" value="<?php echo $r['item_code'];?>" name="itmcode[]"  readonly="readonly"/></td>
 <td><?php echo $r['description'];?></td>
 <td><?php echo $r['qty'];?></td>
 <td><?php echo $r['price_per_qty'];?>
 <td><input type="text" value="<?php echo $r['total_value'];?>" name="tvalue[]" readonly="readonly" /></td>
 <td><?php echo $r['preferred_supplier'];?></td>
<td><input type="checkbox" name="chkbx[]"  value="<?php echo $r['id'];?>">

 //change gid[] to gid[<?php echo $r['id'];?>]

  <input type="hidden" name="gid[<?php echo $r['id'];?>]"  value="<?php echo $r['id'];?>">

  </td>
 </tr>
</table>

in processing

<?php
   if(array_key_exists('chkbx', $_POST)&&(!empty($_POST['chkbx']))&&   (isset($_POST['floatBtn']))){
foreach($_POST['chkbx'] as $rec=>$value)
  {
    $itm = $_POST['itmcode'][$rec];
    $tval = $_POST['tvalue'][$rec];

    $t = $_POST['gid'][$value]; //--> changed from $_POST['gid'][$rec] to $_POST['gid'][$value]

    $apno =$_POST['aNo']; 
    $fno = $_POST['fno'];  
    echo "itm:".$itm." tval: ".$tval." t:".$t." appno:".$apno."fno:".$fno."<br/>";
  }
}
 ?>

The issue when you post the data with the checkbox checked, you get an array like this

Array
(
  [itmcode] => Array
    (
        [0] => 1"
        [1] => 1"
        [2] => 1"
    )

[tvalue] => Array
    (
        [0] => 5
        [1] => 5
        [2] => 5
    )

[chkbx] => Array
    (
        [0] => 1
    )

[gid] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )
)

so when looping the $_POST['chkbx'], the value for `$_POST['gid'][$rec] always loops with the keys of $_POST['chkbx'] which is 0,1,2 etc always. So you get values of $_POST['gid'][0], $_POST['gid'][1],$_POST['gid'][2], etc which is 773, 774, 775 respectively.

Upvotes: 1

Related Questions