如何在php中的POST方法中获取多个值?

时间:2014-02-22 14:05:05

标签: php

---- HTML Form ----

<?php
$query = mysql_query("SELECT * FROM package WHERE status = 0");
while($row = mysql_fetch_array($query)){
$package_id = $row['id'];   
$package_name = $row['name'];   
?>
<tr>
<td><?php echo $package_name; ?></td>
<td><input type="text" name="txt[]" id="quantity" placeholder="0">
 --------- Aboue input is for quantity of those products----------
</td>
</tr>                                                       
<?php 
}
?>

Here is the code for process.php for this form POST method
//
<?php
if(isset($_POST['submit'])){
$alTxt= $_POST['txt'];
$N = count($alTxt); 
for($i=0; $i < $N; $i++){
  echo($alTxt[$i]);
}
}
?>

现在process.php的输出是 125(如果我将输入放在3个文本框中,如1,2,3) 现在问题是我得到了产品的数量,但我怎么能得到每个 产品ID或产品名称以及数量? 所以我可以在process.php中进行计算? 任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

HTML:

<input type="text" name="txt[<?= $product["id"] ?>]" id="quantity" placeholder="0">

请注意,您还可以在HTML中使用嵌套属性:

<input type="text" name="txt[<?= $product["id"] ?>][quantity]" value = "22">
<input type="text" name="txt[<?= $product["id"] ?>][name]" value = "product name">

PHP:

foreach($_POST['txt'] as $productId => $quantity) {
   echo $productId . " " .$quantity;
}