Click to See Complete Forum and Search --> : AJAX - JavaScript/PHP page redirection


intrivious
11-07-2006, 11:22 AM
So I have a get request from the JavaScript calling a PHP script. Anytime after the PHP script has run, I want to relocate the user to an arbitrary page.

What is the proper way to do this? Return "<script> self.location = 'blah.php' </script>" from the php and throw into the page? That just seems a little rigged and won't always work.

Should I set the headers in the PHP?

What do you think?

so_is_this
11-07-2006, 11:41 AM
If JavaScript is successfully making an Ajax call to your PHP script, then there is no need for PHP to be concerned with the redirection (use a hard-coded JavaScript redirect in this case) -- unless you want PHP to inform JavaScript as to the page to which to redirect. In this case, in sounds like you don't really have anything useful to return in the Ajax call. So, just return the url to which JavaScript should redirect and let JavaScript handle it from there.

intrivious
11-07-2006, 12:14 PM
Sounds like a plan. The reason I wasn't doing what you suggested in the first place is, I use the same script for a non-ajax call. So I wanted to keep that working, but it makes more sense just to fix both. Thanks

so_is_this
11-07-2006, 12:26 PM
You can also use the same script for non-Ajax calls. Just have your Ajax call send a special header which your PHP code can check for. I do this with my Ajax calls.

intrivious
11-07-2006, 12:28 PM
Thanks for the info, but what do you mean by a 'special header'?

so_is_this
11-07-2006, 12:37 PM
I issue this in my JavaScript:
objAjax.setRequestHeader('Accept', 'message/x-jl-formresult');
and test for it in my PHP code.

The idea comes from this article:

http://www.jibbering.com/2002/4/httprequest.2002.html