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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.