0

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.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
  • Do you have some more code of cart.php? It does seem like it refreshes itself. All the other errors you post would be solved once it stops doing that. – Dirk Scholten Aug 23 '18 at 09:13
  • Well without a refresh stuff usually doesn’t disappear and get replaced by something else. And that it gets replaced/ overlayed with something else client-side is also rather unlikely in this particular situation, because that would not explain why you’d get undefined index warnings for stuff that supposedly was there further up the same script. As a first step, use your browser dev tools (network panel) to verify whether there is actually just this one single request, or if there’s more happening on that level. – CBroe Aug 23 '18 at 09:14
  • `cart.php` is located next to `index.php` ? so try `action="./cart.php"` – Fky Aug 23 '18 at 09:16
  • @DirkScholten Apparently a friend added a refresh event down in the code without putting it in the change log. My bad for not noticing. The refresh event is needed in order to insert stuff into the database, I'll just insert the stuff i'm adding into the database also and then pull them from the database when needed rather than pulling them from the POST array, unless someone has a smarter way of doing it. Thanks for your input, I honestly went through the code and didn't notice the refresh event that was added, guess you can use that as an answer so I give the green tick, thanks. – Marwan Alkhalil Aug 23 '18 at 09:26

0 Answers0