Jump to content

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;
       }
    ?>

Edited by simpso
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.

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.