weixin_33720452 2015-10-26 05:13 采纳率: 0%
浏览 25

Ajax请求表

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";
?>
  • 写回答

2条回答 默认 最新

  • 程序go 2015-10-26 05:30
    关注

    If you are trying to make your ajax call on submit button onclick event, you need to stop the default form submit event.

    <form onsubmit="return false">
    

    and your submit.php needs POST values, so make a POST request.

    and you also need to pass the variable with that ajax request.

    a nice tutorial on ajax post requests

    评论

报告相同问题?