I am working on an Ajax project. There are many functions that could be implemented if we could run javascript function from php script like this:
<?php
if(isset($_REQUEST['change-div))
{
echo'<script> UpdateDiv("divId"); // function to change the text of the div on ajax page
</script>';
}
?>
But it doesn’t work the javascript code is just echoed as it is and the function doesn’t run. I have tried my best, searched online etc. But there seems no way to do it.
Is there a way to run a JavaScript function from PHP script?
you should manage the code execution on client-side. let say your php script called via ajax is
Then your Ajax success callback (I’m framework agnostic here) could be simply
In this way you will keep client-side and server-side logic well separated. You won’t also need to inject javascript code (changing your DOM structure) or use
eval()