Is there a way to call a JavaScript function from a PHP file using AJAX and the GET method?
I attempted to achieve this by including
<script type="text/javascript"> callFunction(); </script>
but it did not work as expected. Is there an alternative approach to accomplishing this?
function exec(func , arg) {
func(arg); // executes the function
}
If a similar method doesn't exist, how can I successfully call a function with AJAX following a PHP request via the use of the GET method?
The error message I encountered when attempting this was:
'func is not a function'
Here is the snippet of my JS AJAX code:
var emailVal = document.getElementById("email").value;
var passVal = document.getElementById("password").value;
var uVal = document.getElementById("username").value;
var busVar = document.getElementById("businessname").value;
var firstVal = document.getElementById("firstname").value;
var params = "email=" + emailVal + "&password=" + passVal + "&businessname="+busVar + "&username="+uVal+"&firstname="+firstVal;
var url = "business.php";
http = new XMLHttpRequest();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
exececc(http.responseText , null);
}
}
http.send(params);
For reference, here is the relevant PHP code:
echo '<script type="text/javascript"> callFunction(); </script>';