我有一個表單(稍後會添加表單驗證),允許用戶爲房地產網站「添加屬性」。這是我的HTML/JS:AJAX&PHP - 未獲取.responsetext
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add New Property</title>
<script type="text/javascript">
function addProperty() {
var name = document.getElementById('name').value;
var address = document.getElementById('address').value;
var lat = document.getElementById('lat').value;
var lng = document.getElementById('lng').value;
var type = document.getElementById('type').value;
var rent = document.getElementById('rent').value;
var bedrooms = document.getElementById('bedrooms').value;
var owner_id = document.getElementById('owner_id').value;
if (window.XMLHttpRequest) {
//code for IE7+, Firefox, Chrome and Opera
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('message').innerHTML = xmlhttp.responseText;
}
}
var url = "add_property.php?name="+name+"&address="+address+"&lat="+lat+"&lng="+lng;
url += "&type="+type+"&bedrooms="+bedrooms+"&owner_id="+owner_id;
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Property Name:
<input type="text" name="name" id="name" />
<br />
Address:
<input type="text" name="address" id="address" />
Lat: <input type="text" name="lat" id="lat" />
Lng: <input type="text" name="lng" id="lng" />
<br />
Type of property:
<select name="type" id="type">
<option value="house">House</option>
<option value="apartment">Apartment</option>
</select>
<br />
Number of Bedrooms:
<input type="text" name="bedrooms" id="bedrooms" />
<br />
Owner Id:
<input type="text" name="owner_id" id="owner_id" />
<br /><br />
<input type="button" value="Add Property" onclick="addProperty()" />
</form>
<div id="message"></div>
</body>
</html>
這裏是我的PHP:
<?php require_once("includes/initialize.php"); ?>
<?php
$name = $_GET['name'];
$address = $_GET['address'];
$lat = $_GET['lat'];
$lng = $_GET['lng'];
$type = $_GET['type'];
$rent = $_GET['rent'];
$bedrooms = $_GET['bedrooms'];
$owner_id = $_GET['owner_id'];
//this text won't come back...
$message = $name.$address.$lat.$lng.$type.$rent.$bedrooms.$owner_id;
echo $message;
?>
我知道這可能是一個小的語法錯誤,這是我通常不會問關於SO,但我花了2小時以上,我感到非常沮喪。謝謝。
你是否已經證明,請求真正達到你的'add_property.php'腳本?如果不是的話:如果你在'add_property.php'的第一行'死('硬');'你在使用Fiddler/Firebug/whatever-http-proxy-tool中看到了什麼響應? –