无法从$ _POST中检索值

时间:2013-05-30 14:25:57

标签: php

$ _ POST [$ key]不会在我的code.var_dump($ _ POST)中返回一个值给出正确的结果。 当我尝试echo $ _POST [$ key]时,它不会打印任何内容。我正在使用mamp;我读过上一篇关于类似问题但没有解决方案的帖子。这是我的代码...... `

session_start();    

if(isset($_POST["submit"])){
    foreach($_SESSION["cart"] as $key => $qty){         
        if($_POST[$key] == '0'){
            unset($_SESSION["cart"][$key]);
        }
        else            
            $_SESSION["cart"][$key] = $_POST[$key];
        echo $_POST[$key];  
    }
    var_dump($_POST);
}

$cart = $_SESSION["cart"];

>

    <table border="1" cellpadding="10">
    <form action="<? echo $_SERVER["PHP_SELF"];?>" method="post">
    <?  
        if($cart)
        {
    ?>
            <th>Item</th><th>Size</th><th>Quantity</th><th>Unit Price</th><th>Item Subtotal</th>
    <?
            $keys = array_keys($cart);

            foreach($keys as $key) 
            { 
                echo $key;
                $array = explode(".", $key);
                $name = $array[0];
                $size = $array[1];

                $quantity = $cart["$key"];
                echo $quantity;
                $price = $xml->xpath("//item[@name='$name']/price[@size='$size']");
                $unitPrice = (float) $price[0];

                $itemSubtotal = $unitPrice * $quantity;

                $total = $total + $itemSubtotal;
    ?>
                <tr>
                    <td><? print($name); ?></td>
                    <td><? print($size); ?></td>
                    <?  ?>
                    <td align="right"><input type = "text" name="<?print($key)?>" value="<? print($quantity); ?>" size="3"/></td>
                    <td align="right">$<? printf("%0.2f", $unitPrice); ?></td>
                    <td align="right">$<? printf("%0.2f", $itemSubtotal); ?></td>
                </tr>
    <?      
            }
    ?>
            <tr><td colspan="5" align="right"><b>Total: $<? printf("%0.2f", $total); ?></b></td></tr>
    <?
        }
        else
        {
    ?>
            Your cart is empty!
    <?  }  ?>

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

    <p><a href="checkout.php">Checkout</a></p>

`

有谁能告诉我我的代码有什么问题?提前谢谢。

1 个答案:

答案 0 :(得分:-1)

如果您可以发布您正在获得的输出,那就太棒了。

此外,我认为您需要遍历$_POST,而不是$_SESSION["cart"]

foreach($_POST as $key => $qty){

相关问题