Page 1 of 1
Executing Javascript from a PHP script updating from AJAX
Posted: Tue Jan 23, 2007 6:42 am
by impulse()
Do you know if this is possible?
At the moment I have a PHP page with a Javascript function that runs a different PHP page and displays the results back every second using AJAX. That works fine and brings a smile to my face. I want to add something else to this now and be able to run a Javascript alert() function on the PHP page that is called by AJAX from a Javascript function within the PHP page.
If this makes no sense to anybody please say and I'll put more details in so you might be able to understand what I'm trying to ask
Regards,
Posted: Tue Jan 23, 2007 8:11 am
by feyd
The eval() function in Javascript may be of interest.
Posted: Tue Jan 23, 2007 8:44 am
by impulse()
I think I understand.
I have tried putting the Javascript code into a PHP variable:
Code: Select all
$java = "<script type='text/javascript'>";
$java .= "alert('Well, hello there');";
$java .= "</script>";
And then in the Javascript tags outside of the PHP tags I have used:
But this isn't working as expected. Am I misunderstanding eval() or am I not doing it correctly?
Posted: Tue Jan 23, 2007 8:56 am
by feyd
eval() doesn't need the <script> tags, only the Javascript code. However, PHP variables aren't simply transmitted to Javascript. What you need to do is have your Ajax code dig out (in some fashion) the Javascript that needs to be run. Whether this is a separate call to the server-side or apart of the initial data stream is up to you.
Posted: Tue Jan 23, 2007 11:07 am
by impulse()
Would you mind pointing me in the direction of some handy URLs for achieving this? Someone has suggested using JSON which I have only been introduced to this afternoon and from my initial reading I can't see how that will perform what I need to happen.
I have code like this at the moment within a PHP file:
Code: Select all
$code = "alert('Test');";
<script type='text/javascript'>
var foo = "<?php echo $code; ?>";
eval(foo);
</script>
Is that the use of eval you're suggesting? That code runs fine if I load the php directly in a browser. But if that PHP file is loaded through another PHP file making use of AJAX, it fails to execute any Javascript.
Regards
Posted: Tue Jan 23, 2007 1:19 pm
by Kieran Huggins
jquery has this functionality built in. I urge you to take a look.
Posted: Mon Jan 29, 2007 8:50 am
by impulse()
I have been reading though the jQuery documentation but I'm unable to see what would help in this situation. Any advice here?
Regards,
Posted: Mon Jan 29, 2007 11:16 am
by Kieran Huggins
Posted: Mon Jan 29, 2007 11:28 am
by impulse()
I've been doing another full day of research on this and it seems the only problem I am having it that AJAX isn't evaluating the Javascript that is passed to it. I have only found a few cases where somebody tries to explain this but the explinations aren't clear.
What are your views on using eval over jQuery? Taking into account I don't need to do any of the fancy tricks jQuery offers. I only need to run a simple Javascript alert() function within a PHP documents that is updated via Ajax.
Posted: Mon Jan 29, 2007 11:34 am
by Kieran Huggins
that's what $.getScript() is for - it's not the same as $.ajax() for a reason.
Posted: Mon Jan 29, 2007 11:35 am
by impulse()
OK, thanks for the help. My working day has just finished so I'll will take a deep look into jQuery when I get home.
Stephen,
Posted: Mon Jan 29, 2007 4:13 pm
by impulse()
OK, so now I'm going in a straight line with jQuery can I clarify how I need to use jQuery to perform my task? I'm unable to mess around with the script at the minute to test it as it's on my works server which I haven't got access to at the moment.
- I will have a PHP file which will make an AJAX call to another PHP file. That PHP file will have a line like "$.getScript("myAlert.js");" and my alert will have "alert("BOO");" in. And using jQuery should allow me to pass Javascript over to Ajax to execute?
Posted: Mon Jan 29, 2007 4:41 pm
by Kieran Huggins
go to
http://www.visualjquery.com/1.1.1.html
click on [ ajax ], then click on [ $.getScript(url,callback) ]
There you will find the definitive description of the function

Posted: Tue Jan 30, 2007 3:21 am
by impulse()
This is still failing to work. I thought the problem had been solved and I did a victory dance but I realised I was viewing the PHP direct and not through the AJAX script. So I went to the AJAX script and jQuery is failing to run the Javascript I wrote which contains a simple "alert("Smile");". But as I say, going direct to the PHP script that contains the jQuery call works fine.
Any more ideas on this one?
Posted: Tue Jan 30, 2007 5:58 am
by impulse()
I have now solved this with a simple hack. Please bow to the iFrame
I know it's a cheap hack but it works in my situation.
**
It's even made it possible for me to output MySQL database results to a Javascript alert box. I love this.