simpso Posted January 6, 2013 Share Posted January 6, 2013 (edited) 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 January 6, 2013 by simpso Quote Link to comment https://forums.phpfreaks.com/topic/272766-sessions-cart-button/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2013 Share Posted January 6, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272766-sessions-cart-button/#findComment-1403691 Share on other sites More sharing options...
simpso Posted January 6, 2013 Author Share Posted January 6, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/272766-sessions-cart-button/#findComment-1403692 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2013 Share Posted January 6, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/272766-sessions-cart-button/#findComment-1403695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.