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!
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
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.
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:
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.
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.
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?
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.