1

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.

7
  • what is the resulting ids when 777,895 and 901 are checked and clicked add to float? Commented Aug 4, 2016 at 10:12
  • @ArunKrish: it gives 773,774,775 respectively Commented Aug 4, 2016 at 10:16
  • ok the issue will be in the place where you loop and display the data in html table. The code you provided has not enough information on the loop part. You need to check the query section and see whether the results get sorted based on 'id' when you click on Add to Float button when the page posts itself. Commented Aug 4, 2016 at 10:18
  • @ArunKrish: so, if that is not correct, Can you give a correct one or any link to solve this issue? Commented Aug 4, 2016 at 10:20
  • I think I have wrongly understood your question.. Do u want to remove the checked rows when u click Add to Float button? Commented Aug 4, 2016 at 10:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.