组织帖子数组

时间:2011-04-15 20:33:29

标签: php html forms post

这是我的表格

 <form name="input" action="http://localhost/shopper/index.php?route=module/cart/insert_shopper" method="post">
<input quantity="4" type="hidden" name="28" value="1">
<input type="hidden" quantity="3" name="29" value="1">
<input type="submit" value="Submit" />

我有这种格式

  [post] => Array
    (
        [28] => 1
        [29] => 1
    )

我真的希望将post数组作为产品,然后在产品ID上列出数量......是否有一种简单的方法来更改表单

所以在下一页我可以做到这一点

$products = $_POST['products']
foreach( $products as $p)
{
if( isset($p) && $p<>'')
{
///// place your code here
}
}

4 个答案:

答案 0 :(得分:3)

<input type="hidden"  name="products[29][3]" value="1">


$products = $_POST['products']
foreach( $products as $product_id=>$quantity)
{
echo $product_id;
echo $quantity;
}

答案 1 :(得分:2)

在HTML表单中,您可以在name属性值结束后添加数组括号,例如:

<input type = 'hidden' name = 'products[]' value = '12'/>
    <input type = 'hidden' name = 'qty12' value = '16'/>
<input type = 'hidden' name = 'products[]' value = '13'/>
    <input type = 'hidden' name = 'qty13' value = '72'/>
<input type = 'hidden' name = 'products[]' value = '14'/>
    <input type = 'hidden' name = 'qty14' value = '1'/>

然后,接收脚本中的PHP会将$_POST['products']视为一个数组。数量可以通过以下方式看出:

foreach($_POST['products'] as $k => $v) {
    echo($_POST['qty'.$v] . "<br/>");
}

答案 2 :(得分:1)

<form name="input" action="" method="post">
    <input type="hidden" name="29" value="3">
    <input type="hidden" name="28" value="4">
    <input type="hidden" name="27" value="2">
<input type="submit" value="Submit"/>

<?php

if (!empty($_POST))
{
    foreach ($_POST as $id => $quantity)
    {
        echo 'ID: '.$id.' quantity: '.$quantity;
    }
}

答案 3 :(得分:0)

<input type="hidden" name="28" value="4">
<input type="hidden" name="29" value="3">

会给你

[post] => Array
    (
        [28] => 4
        [29] => 3
    )