Code to Run Javascript Code from Ajax Response

Running Javascript Code in Response to a PHP Response Text to an Ajax Call is Extremely Simple to do ...

Home Short:

Within your php code you simple run the echo commmand with the Javascript code you wish to execute. If you intend to provide other php ajax responses that are not intended to run Javascript code, the best way to do this is simply prepend the echo response with js: for example

echo json_encode("js:alert();");.

The json_encode() is required to prevent a javascript error because ajax is expecting a json repsonse.

Then within the Javascript code that handles the ajax response ...

S
H
A
R
E