I have two pages, product.php and viewcart.php. When a user clicks on a button on product.php, it adds the product to the back-end (along with it’s attributes) and goes to viewcart.php. The data gets submitted to viewcart.php through the code below. There are some php variables that need to get passed in addition to a value from a textbox on product.php (the variable is “val” below).
<script language="javascript">
function test()
{
var val=document.getElementById("textarea").value;
var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val;
document.getElementById("a_link").href=hrf;
}
</script>
<a href ="#" id="a_link" onclick="test();" class="btn btn-success" type="submit"><i class="icon-shopping-cart icon-white"></i> Add to Cart</a>
On viewcart.php, the product gets added to the back-end and then gets displayed via the code below:
@$link = $_GET['link'];
$price = $_GET['price'];
$title = $_GET['title'];
$retailer = $_GET['retailer'];
$options = $_GET['options'];
$session = session_id();
$_SESSION['sess_var'] = $session;
//connect to database code here
mysql_query("INSERT INTO sessionid (sessionid, link, retailer, price, title, qt, options) VALUES('$session' , '$link', '$retailer', '$price', '$title', 1, '$options') ");
The problem I am having is when the user refreshes viewcart.php; the product gets added again because of the code above. How do I ensure the product gets added to the database ONLY if the user clicks on the submit button on product.php (and not by refreshing viewcart.php or clicking the “back” button to get to viewcart.php)?
1 Answer