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

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改