我無法讓我的簡單ajax腳本運行。代碼應該很簡單。 了AJAX腳本是:如何使用AJAX將信息發送到PHP腳本?
<html>
<head><title>Testing ajax</title>
<script type="text/javascript">
function ajax() {
var xmlhttp;
var fname=document.getElementById("fname").value;
if(window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("output").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?fname="+fname,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" id="fname">
<input type="text" id="lname">
<input type="submit" id="submit" onclick="ajax()">
</form>
<div id="output"></div>
</body>
而且PHP腳本是:
<?php
$fname=$_GET['fname'];
echo "<p>Hello ".$fname."</p>";
?>
我也試過:
xmlhttp.open("POST","ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/pass-data");
xmlhttp.send("fname="+fname);
我不能既交不獲取發送數據的方法。我沒有看到簡單的東西嗎?
您正在發佈數據,但試圖從$ _GET在PHP中檢索它? – kinakuta
我已經嘗試過這兩個帖子,但無法獲得任何方式的工作。它正在使用得到 – Beamer180
是位於與PHP文件相同的文件夾中的JS文件?路徑是正確的? –