我根据下拉选择从数据库中获取记录。结果显示为一个表格,用户需要勾选他的选择。选择后,用户点击一个按钮,自动添加他的选择现在问题是:当点击按钮时,通过将按钮变为递增数字,为每个项目获取的ID会发生变化。请参见下面的图片:
例如,在下表中,选择了id 773,774,777,895和901.当Add to Float Button被cliked时,Id现在变为:773,774,775,776,777(按升序排列)。见下面的代码: 用于显示表
<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>
处理脚本:
<?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/>";
}
}
?>
如何更正此问题,以便在选择后单击按钮时显示正确的ID。
答案 0 :(得分:1)
像这样更改你的代码
<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>
处理
<?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/>";
}
}
?>
当您选中复选框发布数据时出现问题,您将得到一个类似这样的数组
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
)
)
所以当循环$ _POST [&#39; chkbx&#39;]时,值为 `$ _POST [&#39; gid&#39;] [$ rec]总是循环使用$ _POST [&#39; chkbx&#39;]的键,总是为0,1,2等。所以你得到的值是$ _POST [&#39; gid&#39;] [0],$ _POST [&#39; gid&#39;] [1],$ _ POST [&#39; gid&#39;] [ 2]等,分别为773,774,775。