javascript alert not working if i use php redirect?
i have code written something like this.
if(!isset($cid))
{
echo "<script type=\"text/javascript\">alert('OOps, Something went wrong, please make sure you select country from the list')</script>";
header('Location:location-manager.php?view=countries');
exit;
}
the above code will only redirect the page and ignores the JS alert. if i remove header() then it alerts with JS alert box. what is wrong with my code? isn't it possible for me to alert the user with the error from JS alert开发者_运维问答 and then redirect it to desired page?
why is js alert not working if i use php's redirect?
because you're using output buffering ob_start() placed in your config file. Isn't it was your intention when you turned buffering on - to make browser ignore all text before header? Why do you ask now then?
Using alerts is very bad way to notify user of some action. Worst one I'd say.
And JS redirects is not the way to go, you OUGHT to send 303 status after POST request.
If you want to make no trouble for your users, you have to either
Send your form using AJAX and then notify with some dynamical div.
Use sessions to store alert message, then reload page using
Location:
header, then display message in some dynamical div and delete it from session.
I guess you cannot have any output before header
function. In this case, you have output <script type="text/ja.....
before header. So this should work.
echo "<script type=\"text/javascript\">alert('OOps, Something went".
"wrong, please make sure you select country from the list');".
"window.location = 'http://google.com;'</script>";
EDIT
The other way to alert would be to redirect with an option to show something went wrong.
header('Location:location-manager.php?view=countries&error=true');
And on location-manager.php
if($_GET['error']=='true')
{
echo "Something went wrong";
}
Actually, that code should be throwing an error. You probably either have error reporting disabled or it might be an output buffering thing (?).
In any case, you can't do it with PHP like that. You need to do your redirection with JS
window.location = URL
You have to keep in mind that the the alert happens on the client side after the browser parses it while PHP does the header redirect on the server side. Two very different things.
You can not have any data above the header. No HTML, no javascript, nothing.
I would suggest using pure javascript to redirect the page instead of a header redirect. You can do that by setting window.location
after your alert.
window.location = "http://www.google.com/"
This tutorial shows how to make a time delayed redirect. Example here. You may want to do this to notify your users that they visited the wrong url, and they should change their bookmarks or links pointing to the old url.
If you want header redirection you should use this code, but no javascript will execute.
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com');
Add a space after 'location':
header('Location: http://www.example.com/');
You should add the js message after the redirection, on the new page not before the redirection.
精彩评论