JS Function from PHP file loaded via AJAX

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
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

JS Function from PHP file loaded via AJAX

Post by andrei.mita »

Hello again,

Maybe it's a bit strange what I'm traying to do here but here it goes. I want to call a JS function in a php file that has been loaded via ajax.
I have the index.php where I load the ajax.js scrip.
When I click on Link1 the file1.php is loaded via AJAX in DIV1.
file1.php make an insert in a mysql db, then makes a select then echoes "Done".
At the end it also tryies to call the loadcontent function to load file2.php in DIV2, but nothing happens.
No error, nothing. Is this because the loadcontent function doesn't exist in file1.php from where I try to call it?

If I open file1.php in a pop-up, not in DIV1, and then call the function with window.opener it works.

Please help!
Thanks,
Andrei
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: JS Function from PHP file loaded via AJAX

Post by Hannes2k »

Hi,
you can use the 'eval' javascript function. Your file1.php have to send the javascript you whish to excecute and in the invoking file in the onComplete/onResponse function just call eval on the response of file1.php

But to do so is a bad idea, because you get too many dependencies so thats gonna be hard do modify or extend your script and reusing 'file1.php' in other place is nearly inpossible.

file1.php should only act like a webservice and shouldn't invoke any js functions at client (there where the ajax script is running) site.
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Re: JS Function from PHP file loaded via AJAX

Post by andrei.mita »

file1.php has to be dependent the way it is and it isn't used anywhere else.
The fact is that I need to do something like this. After I click on Link1 a query has to be run and when and if is all done 3 java scripts to be executed.
So I load the php file containing the query, the query is run then try to call the functions.
If I echo a link with onclick event it works, but if a just echo the function nothing happens.
Don't know how eval works.
Post Reply