如何使用表单和PHP将多维数据从一页传递到另一页?

时间:2019-06-25 09:47:04

标签: php html forms

我试图使用表单作为多维数组,将与每个项目相关的多个值从一页传递到另一页。它用于各种购物车。我正在使用表单通过隐藏值传递变量。使用echo输入这些变量的值。变量值在PHP中。我需要在另一个PHP页面上使用这些值。

我需要以表格的形式在第2页中获取此数据,我可以使用orderitem ['quantity']等从中调用数据。但是无论我尝试什么,我都会不断出错。请帮忙。

第1页:

<input type="hidden" name="orderitem[item][]" value="<?php echo $order_cart_item; ?>">
         <input type="hidden" name="orderitem[quantity][]" value="<?php echo $order_cart[$i]['item_q']; ?>">
         <input type="hidden" name="orderitem[price][]" value="<?php echo $row['item_price'];?>">
         <input type="hidden" name="orderitem[total][]" value="<?php echo $row['item_price']*$order_cart[$i]['item_q'];?>">
         <textarea name="orderitem[comments][]" rows="3" cols="35" placeholder="Please mention special requests if you have any. (Example: Happy Birthday Jerry!)"></textarea>

第2页:

 if(($_SERVER["REQUEST_METHOD"] == "GET"))
 {


    $result = $_GET['orderitem'];
    echo "Product ID\tQuantity\tAmount\tTotal\t";
    echo "\n", $result["itemcode"], "\t\t", $result["quantity"], "\t\t",$result["price"], "\t\t", $result["total"];
}
var_dump($_GET['orderitem']);}

变量转储的结果:

Arrayarray(5) { ["item"]=> array(2) { [0]=> string(6) "CC9011" [1]=> string(6) "CCC675" } ["quantity"]=> array(2) { [0]=> string(1) "4" [1]=> string(1) "2" } ["price"]=> array(2) { [0]=> string(3) "900" [1]=> string(2) "60" } ["total"]=> array(2) { [0]=> string(4) "3600" [1]=> string(3) "120" } ["comments"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } }

第2页中的输出:

Product ID  Quantity    Amount  Total   
Notice: Undefined index: itemcode in /opt/lampp/htdocs/momsoven/orderconfirmation.php on line 96

Notice: Array to string conversion in /opt/lampp/htdocs/momsoven/orderconfirmation.php on line 96
Array   
Notice: Array to string conversion in /opt/lampp/htdocs/momsoven/orderconfirmation.php on line 96
Array   
Notice: Array to string conversion in /opt/lampp/htdocs/momsoven/orderconfirmation.php on line 96
Arrayarray(5) { ["item"]=> array(2) { [0]=> string(6) "CC9011" [1]=> string(6) "CCC675" } ["quantity"]=> array(2) { [0]=> string(1) "4" [1]=> string(1) "2" } ["price"]=> array(2) { [0]=> string(3) "900" [1]=> string(2) "60" } ["total"]=> array(2) { [0]=> string(4) "3600" [1]=> string(3) "120" } ["comments"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } }

1 个答案:

答案 0 :(得分:0)

在foreach循环中,$ var是数组的值。您无需使用foreach循环。

<?php
if(isset($_POST["submit"])){
    $result = $_POST['orderitem'];
    echo "Product ID\tQuantity\tAmount\tTotal\t";
    echo "\n", $result["item"], "\t\t", $result["quantity"], "\t\t",$result["price"], "\t\t", $result["total"];
}
?>

如果相同的输入字段具有多个记录,则将输入元素表单添加为数组。

<?php
 if(isset($_POST["submit"])){
$result = $_POST['orderitem'];
echo "Product ID\tQuantity\tAmount\tTotal\t";

    for($i=0 ; $i<count($result['quantity']); $i++){
        echo "\n", $result["item"][$i], "\t\t", $result["quantity"][$i], "\t\t",$result["price"][$i], "\t\t", $result["total"][$i];
    }
 }

?>


<form name="test" method="post">

    <input type="hidden" name="orderitem[item][]" value="order_cart_items">
    <input type="hidden" name="orderitem[quantity][]" value="item_q">
    <input type="hidden" name="orderitem[price][]" value="item_price">
    <input type="hidden" name="orderitem[total][]" value="item_price">
    <textarea name="orderitem[comments][]" rows="3" cols="35" placeholder="Please mention special requests if you have any. (Example: Happy Birthday Jerry!)"></textarea>


    <input type="hidden" name="orderitem[item][]" value="order_cart_items">
    <input type="hidden" name="orderitem[quantity][]" value="item_q">
    <input type="hidden" name="orderitem[price][]" value="item_price">
    <input type="hidden" name="orderitem[total][]" value="item_price">
    <textarea name="orderitem[comments][]" rows="3" cols="35" placeholder="Please mention special requests if you have any. (Example: Happy Birthday Jerry!)"></textarea>



    <input type="submit" name="submit" value="submit"> 
</form>