Back javascript with AJAX

Recently, the developer told me that I can make ajax requests to php pages, return javascript methods to them, and then execute these methods by the client. How it's done? Linking to related resources will also be great.

+4
source share
1 answer

An example can be executed using jQuery (javascript library).
If you call ajax request:

$.ajax({
    url:"phpfile.php",
    type:"post",
    data: {id: 4},
    async:true,
    success: function(data) {
        $("div").html(data);
    },
    error: function() {
        alert("Error");
    }
});

and in phpfile.php you echosome javascript code it can be executed:

<?php
echo "
<script>
jQuery(document).ready(function() {
    $(\"#someDiv\").click(function() {
        alert(\"#someDiv clicked\");
    });
});
</script> ";
?>
+1
source

All Articles