arunpatal Posted March 1, 2014 Share Posted March 1, 2014 (edited) When i don't use header then javascript runs but after adding header (below javascript) javascript is not working if(isset($_POST["update_cart"])){ foreach($_POST["quantity"] as $pid => $qty): @array_splice($_SESSION["my_cart"][$pid] = $qty); if ($_SESSION["my_cart"][$pid] < 1 ){ unset ($_SESSION["my_cart"][$pid]);} elseif ($_SESSION["my_cart"][$pid] > 10){ $_SESSION["my_cart"][$pid] = 10; echo "<script> alert('quantity cannot be more then 10') </script>"; }; endforeach; header("location:" . $_SERVER["PHP_SELF"]); }; Edited March 1, 2014 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/286636-header-location/ Share on other sites More sharing options...
ginerjm Posted March 1, 2014 Share Posted March 1, 2014 You really don't want to use JS to issue an error message from within your php script. That's why header doesn't work, which you would probably know if you had error checking turned on. Design your script to re-issue your page with an error message area that you can then populate with messages and re-display the page so that the user can make corrections. Quote Link to comment https://forums.phpfreaks.com/topic/286636-header-location/#findComment-1471173 Share on other sites More sharing options...
r3wt Posted March 2, 2014 Share Posted March 2, 2014 (edited) if you want to redirect without issuing the die function or exit function, you can use the following which i use on my website to keep it from glitching and continue so i can process the error and continue executing code later if i need to. if( /* CONDITIONS HERE */){ echo '<meta http-equiv="refresh" content="0; URL=http://example.com/page.php">'; } for example if you want to save some time by passing the error to the next page, or if you need to regenerate some form data: $error = false; $errors = array(); //statement that failed if(hash('sha512',$enteredpass . $salt) != $storedpass){ $error = true; } if($error === true){ $error_id = "somerandomlongstring"; //generate error message foreach($error as $errors[]) { $message = "Error: username or password invalid"; $user = $_POST["user"]; $ip = getIp(); $query = mysql_query("INSERT..."); } echo '<meta http-equiv="refresh" content="0; URL=index.php?page=login&error=".$error_id."">'; } then on the page you do a query to get the error string if(isset($_GET["error"])){ //query the results. $sql = mysql_query....; $num_rows = mysql_num_rows($sql); for($i = 0; $i < $num_rows; $i++){ $errors = array(); $error[$i] = mysql_result($sql,$i,"message"); echo $error[$i]."</br>"; } Edited March 2, 2014 by r3wt Quote Link to comment https://forums.phpfreaks.com/topic/286636-header-location/#findComment-1471199 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.