brash21 Posted September 15, 2007 Share Posted September 15, 2007 Ive been playing around with a simple shopping cart and sessions. I want the script to echo the amount everytime someone hits the add to cart button. So I would like the script to keep adding the amounts even for when someone repeatedly hits the button. heres my script: In the head i have: <script> <? session_start(); $toupeeqty = $_POST['toupeeqty']; $_SESSION['toupee'] = "$toupeeqty"; if(isset($_SESSION['toupee'])) $_SESSION['toupee'] = $_SESSION['toupee'] + $toupeeqty; else $_SESSION['toupee'] = $toupee; ?> and heres what i have in the body: <img src = "toupee.jpg" /><br/> <form action = "question1.php" method = "post"> How many toupees would you like?<br/> <input type = "text" name = "toupeeqty" value = "1"/> <input type = "submit" name = "submit" value = "Add to cart" /> </form><br/><br/> </br><br/> Click image to preview the Wild Wacky Action Bike!<br/><br/> <img src = "wacky.jpg" onclick="previewbike();"/><br/> <br/><br/> <form action = "question1.php" method = "post"> How many wild wacky action bikes would you like?<br/> <input type = "text" name = "wackybikeqty" value = "1"/> <input type = "submit" name = "submit" value = "Add to cart" /> </form><br/><br/> <? echo $_SESSION['toupee']; ?> Everytime i enter an amount the script just doubles the value. how do i get the script to keep adding amounts entered? thanks in advance Link to comment https://forums.phpfreaks.com/topic/69421-help-with-sessionssimple-shopping-cart/ Share on other sites More sharing options...
syntaxerror Posted September 15, 2007 Share Posted September 15, 2007 from what i see, every time you hit your the first block or page, you're initializing the toupeeqty in your session variable to what was once previously posted. , you have to make a separate page/block where you initialize the session variable only once eg like if (isset($_SESSION['toupeeqty']){} else{ $_SESSION['toupeeqty'] = $_POST['toupeeqty']; } perhaps just initialize the $_SESSION['toupeeqty'] to zero from an initial page/mainpage after login, and then on all other pages where add to cart is available you'd just do your add block, and not have to reintialize the value of $_SESSION['toupeeqty'] Link to comment https://forums.phpfreaks.com/topic/69421-help-with-sessionssimple-shopping-cart/#findComment-348811 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.