I now have a simple "shopping cart" script that I have converted to an "add to shortlist" script to better suit my website.
Everything wokrs great however after you click the add to shortlist link it goes to the shortlist page.
Here is the code for "add to shortlist"
<a href = "http://www.xxxxxxxx.com/shortlist.php?action=add&id=' . $id . '" <b>Add to Shortlist<b> </a>
Is it possible to stay on the page after this is clicked rather than going straight to the shortlist page?
Here is the code for my shortlist page.
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
<!--
body {
background-color: #E8E1C3;
}
-->
</style>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".flip").click(function(){
$(this).next('.panel').slideToggle('slow');
});
});
</script>
<title>Shortlist</title>
<?php
include("header_1.php");
include ("Connections.php");
include("header.php");
include("heading.php");
?>
</head>
<body>
<div id='content'><div id='output'>
<?php
$product_id = (empty($_GET['id']) ? 0 : $_GET['id']); //the product id from the URL
$action = (empty($_GET['action']) ? 0 : $_GET['action']); //the action from the URL
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) { //decide what to do
case "add":
$_SESSION['cart'][$product_id] = (array_key_exists($product_id, $_SESSION['cart'])) ? $_SESSION['cart'][$product_id] +1 : 1; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
?>
<?php
if(isset($_SESSION['cart'])) { //if the cart isn't empty
//show the cart
echo "<table border=\"0\" padding=\"0\" width=\"100%\">"; //format the cart using a HTML table
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT name, colour, sex FROM employees WHERE id = %d;",
$product_id);
$result = mysql_query($sql);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($name, $colour, $sex ) = mysql_fetch_row($result);
echo "<tr>";
echo "<td align=\"left\" > <span class=\"style40\" >$name </span class> <span class=\"style90\" >$colour $sex $dob</span class></td>";
echo "</tr>";
echo"</tr>";
echo"<tr>";
echo "<td align=\"center\"> <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">Remove from shortlist</a></td>";
echo "</tr>";
}
}
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">CLEAR ALL FROM SHORTLIST</a></td>";
echo "</tr>";
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shortlist.";
}
//function to check if a product exists
function productExists($product_id) {
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM test WHERE id = %d;",
$product_id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
<a href="test.php">Continue Browsing</a>
</div></div>
<?php include ("footer.php"); ?>
</body>
</html>