Jump to content

PHP Shopping Cart


DreamOn

Recommended Posts

Hi All

 

I have my shopping cart working correctly, just a small tweak which i would like to make to it.

 

Here is the code i currently use...

echo '<p>' . $fldname . ' <a href="cart.php?action=add&id=' . $row['idinfo'] . '">Add to report</a></p>';

 

But i want it so that when i click 'Add to report' is does not take me to cart.php, but it still adds the item into the shopping cart.

 

kind of just refeshs the current page?

 

Im sure it is only a small tweak which needs to be done..

 

Any help would be great

 

-Dream

Link to comment
https://forums.phpfreaks.com/topic/230467-php-shopping-cart/
Share on other sites

Move the add function from the cart.php file to the product file or wherever the Add to report link is. Then on click execute the function when the page reloads. You could also do it with AJAX without a page refresh. Switching to ajax would be more than a small tweak though.

Link to comment
https://forums.phpfreaks.com/topic/230467-php-shopping-cart/#findComment-1186786
Share on other sites

Thanks for the reply Scotty.

 

You need to bare with me with this as i am fairly new to PHP, about 1-2month.

 

this is the code which was in cart.php

 

$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
}

 

i have added this to the top of index.php

 

and changed the link to the following:

 

echo '<p>' . $fldname . ' <a href="' . $_SERVER['PHP_SELF'] . '?viewdetail=' . $nameid . '&action=add&id=' . $row['idinfo'] . '">Add to report</a></p>';

 

but still cant get this to work, can you see where im going wrong?

 

-Dream.

Link to comment
https://forums.phpfreaks.com/topic/230467-php-shopping-cart/#findComment-1186795
Share on other sites

Try something like this:

 

<html>
<head>
<title></title>
</head>

<body>
<?PHP
session_start();

$cart = $_SESSION['cart'];
$action = $_GET['action'];

//test $action
echo 'Value of action: ' . $action;

switch ($action)
{
  case 'add':
  if ($cart) {
   $cart .= ','.$_GET['id'];
  } else {
   $cart = $_GET['id'];
  }
  break;
}
?>

<p><?=$fldname;?> <a href="<?=$_SERVER['PHP_SELF'];?>??viewdetail=<?=$nameid;?>&action=add&id=<?=$row['idinfo'];?>">Add to report</a></p>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/230467-php-shopping-cart/#findComment-1186814
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.