SpokaneDude Posted July 15, 2015 Share Posted July 15, 2015 (edited) I have some PHP code that does eCommerce; I have a 'Continue Shopping' button with this code: if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['productcode'])) { AddToCart(); } else { $action = isset($_POST['action']) ? $_POST['action'] : ''; $value = strtoupper(substr($action, 0, 5)); switch ($value) { // continue shopping case "CONTI": header("Location: "."index.html"); break; which is not working - status on bottom of Firefox browser just says: waiting for sitename.com and nothing ever happens. So, what is the correct way to go to another page from the current page? BTW, I'm a noob (but I'm learning) at PHP, so please excuse the lame question. Edited July 15, 2015 by SpokaneDude Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/ Share on other sites More sharing options...
requinix Posted July 15, 2015 Share Posted July 15, 2015 You'll have to figure out what is taking so long (infinite loop?) in PHP - what the browser says is irrelevant. Nothing in the code you've posted would be at fault so look elsewhere. Maybe AddToCart? The rest of the code you didn't post? Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516453 Share on other sites More sharing options...
Ch0cu3r Posted July 15, 2015 Share Posted July 15, 2015 which is not working - status on bottom of Firefox browser just says: waiting for sitename.com and nothing ever happens. That to me seems like your code is stuck in a loop or something is taking a long time to process. The code you posted is fine and will redirect the user, provided you are not outputting anything before the use of header If the only purpose of the continue shopping button is to redirect the user back to your store, then why not just use a link which links back to your stores homepage. If you want the link to appear as a button you can style it using css. Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516454 Share on other sites More sharing options...
SpokaneDude Posted July 15, 2015 Author Share Posted July 15, 2015 (edited) That's exactly what I was trying to do - redirect the user back to the home page (index.html) and they can go from there... where do I find out how to "style it using CSS"? (I'm a noob at CSS also, but I do know other languages). Here is the complete code for the View Cart page: <?php session_start(); define("PRODUCTCODE", 0); define("PRODUCTNAME", 1); define("QUANTITY", 2); define("PRICE", 3); define("URL",4); if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['productcode'])) { AddToCart(); } else { $action = isset($_POST['action']) ? $_POST['action'] : ''; $value = strtoupper(substr($action, 0, 5)); echo $value; switch ($value) { // continue shopping case "CONTI": header("Location: "."index.html"); // header("Location" ".$GET['referringURL']); break; // recalculate case "RECAL": RecalculateCart(); break; // proceed to checkout case "CHECK": header("Location: "."customer.php"); break; } } } function AddToCart() { $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : ''; $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0; $cart[PRODUCTCODE][$itemcount] = $_POST['productcode']; $cart[PRODUCTNAME][$itemcount] = $_POST['productname']; $cart[QUANTITY][$itemcount] = intval($_POST['quantity']); $cart[PRICE][$itemcount] = $_POST['price']; $itemcount = $itemcount + 1; $_SESSION['cart'] = $cart; $_SESSION['itemcount'] = $itemcount; } function RecalculateCart() { $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : ''; $itemcount = isset($_SESSION['itemcount']) ? $_SESSION['itemcount'] : 0; for ($i=0; $i<$itemcount; $i++) { $quantity = $_POST['quantity'.($i)]; if (empty($quantity)) { $quantity = 0; } else if (($quantity < 0) || (!is_numeric($quantity))) { $quantity = 0; } $cart[QUANTITY][$i] = intval($quantity); } for ($j=0; $j<$itemcount; $j++) { $quantity = $cart[QUANTITY][$j]; // remove item from the cart if ($quantity == 0) { $itemcount--; $curitem = $j; while(($curitem+1) < count($cart[0])) { for ($k=0; $k<4; $k++) { $cart[$k][$curitem] = $cart[$k][$curitem+1]; $cart[$k][$curitem+1] = ''; } $curitem++; } } } $_SESSION['itemcount'] = $itemcount; $_SESSION['cart'] = $cart; } ?> Edited July 15, 2015 by SpokaneDude Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516460 Share on other sites More sharing options...
scootstah Posted July 15, 2015 Share Posted July 15, 2015 Like this. Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516464 Share on other sites More sharing options...
SpokaneDude Posted July 15, 2015 Author Share Posted July 15, 2015 @scootstah let me work with that and I'll get back to you (probably tomorrow)... it's not a case of drag and drop the code, I have to rework the entire bit of PHP... thank you for your post, tho' SpokaneDude Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516467 Share on other sites More sharing options...
SpokaneDude Posted July 17, 2015 Author Share Posted July 17, 2015 The problem appears to be that the variable associated with the POST action ($value) is not set properly in the code above. If I take the switch statement and add the 'default' to it with the following statement, it works: header("Location: "."index.html"); if I add this statement to it under 'default', header("Location" ".$GET['referringURL']); it causes an error (blank page) So the question is: why is the variable not being set? Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516670 Share on other sites More sharing options...
requinix Posted July 17, 2015 Share Posted July 17, 2015 if I add this statement to it under 'default', header("Location" ".$GET['referringURL']); it causes an error (blank page) Probably the glaring syntax error. You're also missing a colon. Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516671 Share on other sites More sharing options...
Solution mac_gyver Posted July 18, 2015 Solution Share Posted July 18, 2015 (edited) in addition to that, it's $_GET not $GET, and you are concatenating incorrectly. btw - your cart definition is overly complicated, requiring a huge amount of code to maintain. all you should store in the cart is the productcode as an array index and the quantity as the value. you can do this directly in a session variable - $_SESSION['cart']. there's no need to copy it to another php variable, then copy it back. once you simplify your cart definition, to get a count of the different items in the cart, you can just use count($_SESSION['cart']). to get the total quantity of items, you can just use array_sum($_SESSION['cart']). when you need to display the cart, by retrieving the information for all the productcodes that are in the cart, you can just use array_keys($_SESSION['cart']) to get all the productcodes. Edited July 18, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/297311-trying-to-go-to-another-page/#findComment-1516674 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.