所以我一直在学习使用 xmlhttp,但我无法让这个简单的脚本工作:
<!DOCTYPE html>
<html>
<head>
<script>
function print_stuff(){
document.getElementById("two").innerHTML="working";
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("one").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","index.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("email=" + document.getElementByName("email").value + "&name="+ document.getElementByName("name").value);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
Name: <input type="text" name="name"/></br>
Email:<input type="text" name="email"/></br>
<button onclick="print_stuff()">Button</button></br>
<span id="one"></span>
<div id="two"></div>
</body>
</html>
和 index.php:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo "Name: ",$name,"</br> Email: ", $email;
?>
这背后的想法非常简单:您获取用户名和电子邮件并使用“POST”方法将其打印出来。我有一种感觉,这是一个非常简单的错误,虽然我找不到它......任何帮助表示赞赏!