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.