Here are the links to my web page and source code. If you notice, I have a "Form". Once the user submits the application, the server gives him a reply through the file "submit.php". You can verify this online.
I'm trying to use that server reply by using AJAX request method to retrieve it, so i can post the same reply under right under the form once the user clicks on the button. I'm a newbie to web development, and I'm not sure what is wrong with my AJAX code. Any help would be appreciated. My submit.php code is below. For the whole directory click here
<?php
$name = $_POST['name'];
$email=$_POST['email'];
$bday = $_POST['bday'];
echo "Reply from the server:";
echo "<br>";
echo "Thanks for your interest in our product!";
echo "<br>";
echo "We have filed the following Info:";
echo "<br>";
echo "Name: ";
echo "$name";
echo "<br>";
echo "E-mail: ";
echo "$email";
echo "<br>";
echo "Birthday: ";
echo "$bday";
?>
askServer is called upon submit button onclick event
var req = new XMLHttpRequest();
function askServer() {
req.open("GET", "submit.php", true);
req.onreadystatechange = handleServerResponse;
req.send();
document.getElementById("Sreply").innerHTML = "The request has been sent.";
}
function handleServerResponse() {
if ((req.readyState == 4) && (req.status == 200))
{
var Sreply =
req.responseText;
document.getElementById("Sreply").innerHTML = "The answer is: " + Sreply;
}
}
The below form is for the respondent @gaurav
<?php
$name = $_POST['name'];
$email=$_POST['email'];
$bday = $_POST['bday'];
echo "Reply from the server:";
echo "<br>";
echo "Thanks for your interest in our product!";
echo "<br>";
echo "We have filed the following Info:";
echo "<br>";
echo "Name: ";
echo "$name";
echo "<br>";
echo "E-mail: ";
echo "$email";
echo "<br>";
echo "Birthday: ";
echo "$bday";
?>