Jump to content

Sessions cart button


simpso

Recommended Posts

HI everyone i have been messing about with creating a sessions shopping cart using arrays all day and i finally have it working but wanted to see if anyone knew of a smarter way to do it, possibly without having to leave the page?

 

Basically i have an add to cart button with the product id within the hyper link. When clicked this takes you to a page called addtocart.php using $GET i take the id and add to $_session which runs on this page.

 

the html then redicrects you back page you were on.

 

Any ideas?

 

Thanks

 

Page 1 code

 <a href="addtocart.php?id=<?PHP echo $row_ItemsList['ID']; ?>" >Add to cart</a>       

 

Page 2 code

<?php
$id = $_GET['id'];

   if(isset($_SESSION['cartid']))
       {
        $entry = count($_SESSION['cartid']);
        $_SESSION['cartid'][$entry]['productid'] = $id;
       }else
       {
           $_SESSION['cartid'] = array();
           $_SESSION['cartid'][0]['productid'] = $id;
       }
    ?>

Link to comment
https://forums.phpfreaks.com/topic/272766-sessions-cart-button/
Share on other sites

If you want to do something on a web page without refreshing/redirecting, you need to use AJAX to make the http request. Jquery is a popular javascript library that will let you easily write client-side code to make ajax requests and display the returned information on a web page.

If you want to do something on a web page without refreshing/redirecting, you need to use AJAX to make the http request. Jquery is a popular javascript library that will let you easily write client-side code to make ajax requests and display the returned information on a web page.

 

Is ajax difficult to add in, would i need to manipulate my code much to get it to work with?

It's straight forward, but you must specifically structure your client-side code to use ajax on a web page. The server-side code doesn't change much. It still accepts a http request, validates input, produces a result, and outputs it back to the client. The format of the output needs to take into account how it will be used in the client (is it just a block of text that is displayed or is it data that the javascript uses for some purpose, in which case you would output it as JSON encoded data.)

 

There are countless examples posted all over the Internet.

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.