PHP and AJAX problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yadafarin
Forum Newbie
Posts: 2
Joined: Fri Mar 23, 2007 3:16 am

PHP and AJAX problem

Post by yadafarin »

hi
I have problem with AJAX and php.
In .htm file: I load content of a .php file into my layer (<div ...).
the major my script of .php file is:
header("Locatoion: /search.php");
i want .htm file is redirect to /search.php .
But /search.php is load into layer and .htm doesn't redirect.
Can u help me.
thanks
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

you want to load some ajax content that can reload the whole page with a different URL?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

When you load a PHP script via XMLHttpRequest, it's a separate request from that of the page making the AJAX request. You'll need to handle the redirect with Javascript using

Code: Select all

location.href="http://mysite.com/page.htm";
yadafarin
Forum Newbie
Posts: 2
Joined: Fri Mar 23, 2007 3:16 am

RE: aaronhall

Post by yadafarin »

thank u Kieran Huggins but aaronhall:
I added this line to .php file:

Code: Select all

echo '<script language="javascript">location.href="/search.php";</script>';
This script load in my layer (<div ...) but .htm file should be refresh to run this script.
Can I use a method to running this script when .php is load?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

This won't always work as you expect.

I strongly encourage you to use jQuery and the $.getScript("test.js"); method
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Again, the redirect needs to be handled from the script calling the AJAX script, not from the script being called. The location.href expression can be placed somewhere after the javascript function making the AJAX request, preferably in the call's "onSuccess" event handler.
Post Reply