views:

29

answers:

2

I've got a PHP file that is being called to fill in a DIV via ajax. Everything works fine and dandy except I can't for the life of me get a JS alert box to pop up from the PHP.

Here is what I'm using to test (this is being tacked on toward the end of the PHP, I've also tried it toward the beginning, in the middle, etc):

 echo "does this show up?";
 echo "<script language='javascript'>alert('thanks!');</script>";  

"does this show up?" is echoed and I can see the JS in codeinspector but no alert box. If I take that code and throw it in its own PHP file (so it is no longer embedded as part of a larger ajax app) it works just fine.

Any suggestions?

A: 

javascript is parsed once during page load. So if u put these code in page it works fine.... Browser does not parse on ajax loaded content...but we can make javascript function calls from ajax loaded content.

Deepak
+2  A: 

Sending back a script block via AJAX is the wrong way to do it. Instead, you should return a value (probably JSON using json_encode and then decode using the javascript JSON library) and then call alert from your script.

For example, something like this (untested, but you get the idea):

PHP:

echo json_encode(Array('OperationSucceeded' => 'true', 'Message' => 'test'));

Javascript:

retValObj = JSON.parse(retVal);
if (retValObj.Message != null) {
    alert(retValObj.Message);
}
Nate Pinchot