dpqaaczn141761 2018-08-24 18:58
浏览 31

PHP变量打破AJAX代码

I am adding a textarea inside a form that is within a table in order for users to write comments for their order. My application is a shopping cart app, and everything works well, until I add a particular PHP variable. I will start with the code.

AJAX code to add item to cart on index.php:

if(product_quantity > 0)  
       { 
            $.ajax({  
                 url:"action.php",  
                 method:"POST",  
                 dataType:"json",  
                 data:{  
                      product_id:product_id,   
                      product_name:product_name,   
                      product_price:product_price,   
                      product_quantity:product_quantity,   
                      action:action  
                 },  
                 success:function(data)  
                 {  
                      $('#order_table').html(data.order_table);  
                      $('.badge').text(data.cart_item);  
                      alert("Product has been Added into Cart");  
                 }  
            });  
       }  

action.php:

$order_table .= ' 
            <tr>
            <td colspan="3" align="right"><span style="font-size:1.3em;">Tax</span></td>
            <td align="right">$'.number_format($tax,2).' </td> 
            </tr>
            <tr> 
                 <td colspan="3" align="right"><span style="font-size:1.3em;">Total</span></td>  
                 <td align="right">$ '.number_format($total, 2).'</td>  
                 <td></td>  
            </tr>  
            <tr>  
                 <td colspan="5" align="center">  
                 <form method="post" action="cart.php">
                        <textarea name="comments" class="form-control" placeholder="Please enter any special instructions for the order">
                        '.$_SESSION["comment"].'
                        </textarea> <br>
                           <input type="submit" name="place_order" class="btn btn-warning" value="Place Order" />  
                        </form>
                 </td>  
            </tr>  
       ';  
  }  
  $order_table .= '</table>';  
  $output = array(  
       'order_table'     =>     $order_table,  
       'cart_item'          =>     count($_SESSION["shopping_cart"])  
  );  
  echo json_encode($output);

The problem is that when adding the piece of code in action.php $_SESSION["comment"] in the text area in order to preserve what customer already wrote, the application stops prompting the alert on the index page alert("Product has been Added into Cart"); and also stops showing that there are items added to cart when navigating to the cart tab, therefore the table does not show at all even though the items do get added to the actual session variable AKA the cart. Any ideas? Thanks you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入