Executing Javascript from a PHP script updating from 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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Executing Javascript from a PHP script updating from AJAX

Post 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,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The eval() function in Javascript may be of interest.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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:

Code: Select all

eval($java);
But this isn't working as expected. Am I misunderstanding eval() or am I not doing it correctly?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

jquery has this functionality built in. I urge you to take a look.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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,
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

take a look at $.getScript();

http://www.visualjquery.com/new.html
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

that's what $.getScript() is for - it's not the same as $.ajax() for a reason.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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,
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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 :-)
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
Post Reply