cart.php items.phpThe following code displays products in the cart of a user.using the submit buttons user can delete individual items in the cart.It is saved in cart.php file.This code works fine.
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div class="txt-heading">Shopping Cart</div>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM cart where ip='".$ip."'";
$result = mysqli_query($conn, $sql);
$item_total =0;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?><form method="post" action="" id="cart1">
<tr>
<td><strong><?php echo $row["name"]; ?></strong></td>
<td><?php echo $row["qty"]; ?></td>
<td align=right><?php echo "Rs.".$row["price"]; ?></td>
<input name="no" type="hidden" value="<?php echo $row["orderno"] ?>" />
<td><input type="submit" name="submit" id="submit"/></td>
</tr>
</form>
<?php
$item_total += ($row["price"]*$row["qty"]);
}
}
?>
<?php if ($_POST['submit']) {
$or=$_POST['no'];
$sql = "delete FROM cart where orderno='".$or."'";
$result = mysqli_query($conn, $sql);
header("Location: ".$_SERVER['PHP_SELF']);}
?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "Rs.".$item_total; ?></td>
</tr>
</tbody>
</table>
</body>
</html>
The problem is that when i load this into a div of another page items.php using
<script>
$(document).ready(function(){
setInterval(function(){
$("#cart").load('cart.php')
}, 20);
});
</script>
The cart.php is properly loading in the div #cart, but when i click on submit nothing happens. Please note that on clicking submit in cart.php the product was deleted from the database.
Can anybody please explain how to fix this problem?