Jump to content

Recommended Posts

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 by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/286636-header-location/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/286636-header-location/#findComment-1471173
Share on other sites

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 by r3wt
Link to comment
https://forums.phpfreaks.com/topic/286636-header-location/#findComment-1471199
Share on other sites

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.