douchao1957 2018-08-23 09:02
浏览 8

POST数组不保留变量

I am trying to process form fields via POST method. I will post a long code for you to inspect, however the structure of the code is simply like this

<form> <table> <th> </th> <td> </td> </table> </form>

so it is a table inside a form. When the form is submitted, I am taken to the "cart.php" page which is expected, but that page shows the echo statement and shows the variables correctly for a split second, then shows the rest of the page but the variables in the echo statement become "undefined". P.S: Please don't mark as duplicate with this question, thanks.

This is the first page index.php

<div class="table-responsive" id="order_table">  
<form method="POST" action="cart.php" class="form-group">
    <table class="table table-bordered">  
        <tr>  
                <th width="40%">Product Name</th>  
                <th width="10%">Quantity</th>  
                <th width="20%">Price</th>  
                <th width="15%">Sub Total</th>  
                <th width="5%">Action</th>  
        </tr>  
        <?php  
        if(!empty($_SESSION["shopping_cart"]))  
        {  
                $sub_total = 0;  
                foreach($_SESSION["shopping_cart"] as $keys => $values)  
                {                                               
        ?>  
        <tr>  
            <td><?php echo $values["product_name"]; ?></td>  
            <td><input type="text" name="quantity[]" id="quantity<?php echo $values["product_id"]; ?>" value="<?php echo $values["product_quantity"]; ?>" data-product_id="<?php echo $values["product_id"]; ?>" class="form-control quantity" /></td>  
            <td align="right">$ <?php echo $values["product_price"]; ?></td>  
            <td align="right">$ <?php echo number_format($values["product_quantity"] * $values["product_price"], 2); ?></td>  
            <td><button name="delete" class="btn btn-danger btn-xs delete" id="<?php echo $values["product_id"]; ?>">Remove</button></td>  
        </tr>  
        <?php  
            $sub_total = $sub_total + ($values["product_quantity"] * $values["product_price"]);
            $tax = $sub_total * 0.075;
            $total = $sub_total + $tax;  
        }  
        ?>  
        <tr>  
            <td colspan="3" align="right"><span style="font-size:1.3em;">Tax</span></td>
            <td align="right">$ <?php echo number_format($tax,2); ?></td>
            </tr>
            <tr>
            <td colspan="3" align="right"><span style="font-size:1.3em;">Total</span></td>  
            <td align="right">$ <?php echo number_format($total, 2); ?></td>  
            <td></td>  
        </tr>  
        <tr>  
            <td colspan="5" align="center">  

                    <textarea name="comments" id="comment" class="form-control" placeholder="Please enter any special instructions for the order"></textarea> <br>
                    <input type="submit" name="place_order" id="place_order" class="btn btn-warning" value="Place Order" />  
            </td> 
        </tr>  
        <?php  
        }  
        ?>  
    </table> 
    </form> 

And this is the beginning of cart.php:

 $comment = $_POST['comments'];

 echo $comment. $_POST['place_order'];
 print_r($_POST);

How I know that the variables do get passed is cause I noticed some result appearing for a split second so I took a video and paused it there and it had the expected POST array, before the page suddenly appears as expected but with

"Undefined index: comments "

and

"Undefined index: place_order"

and "Array()" at the top. cart.php does not have any commands that make it refresh. Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算