1 | initial version |
There are a few ways that a response can be obtained using PHP and JavaScript:
Example:
JavaScript code:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("GET", "path/to/php/script.php", true);
xhttp.send();
PHP code:
<?php
// Do some processing here
$response = "Hello from PHP!";
echo $response;
?>
Example:
JavaScript code:
fetch('path/to/php/script.php')
.then(response => response.json())
.then(data => console.log(data));
PHP code:
<?php
// Do some processing here
$response = array('message' => 'Hello from PHP!');
echo json_encode($response);
?>
These are just two examples of how to obtain a response using PHP and JavaScript, there are many other ways to achieve this depending on your specific use case.