1

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?

5 Answers 5

2

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.

1
  • i like the first method, anyhow i plan to use ajax. that way i could could display error in a better way. thank you for suggesting alternatives. Apr 3, 2011 at 9:37
1

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";
}
3
  • Javascript (or any content for that matter) will not work after a redirect.
    – Philip
    Apr 3, 2011 at 7:10
  • Unless javascript is disabled in which case the user will be sitting there wondering what's going on
    – meouw
    Apr 3, 2011 at 7:11
  • @Philip well, i do not intent to redirect at all (using php). And i am alerting before redirecting .. moreover it works.
    – S L
    Apr 3, 2011 at 7:12
1

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.

4
  • 1
    The header command actually does get sent to the client. The browser parses it and redirects on the client side.
    – Philip
    Apr 3, 2011 at 6:55
  • nope, my error reporting is on yes i am using output buffering ob_start() is placed in my config file. oh and thank you for explaining i got it. :) Apr 3, 2011 at 7:01
  • @Philip Yup, you're right of course. Apache sends a status status 3xx to the browser. Don't recall the exact code.
    – JohnP
    Apr 3, 2011 at 7:02
  • Most common is "301 Moved Permanently", but there's a bunch of other 3xx codes too as you mentioned :)
    – Philip
    Apr 3, 2011 at 7:08
0

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');
3
  • will it not work even if i use ob_start(), i have included that already in my config file place above. Apr 3, 2011 at 6:58
  • 1
    No, because the browser will get the redirect header, then immediately send a request to the redirect url and completely ignore any content that you send after that.
    – Philip
    Apr 3, 2011 at 7:00
  • I believe there is also an error in your header setting php code (although the browser may figure out what you mean and do it anyway). See my edit for the correct header.
    – Philip
    Apr 3, 2011 at 7:07
-1

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.

1
  • 1
    that is not the problem here. The problem is mixing the JS and header redirects.
    – JohnP
    Apr 3, 2011 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.