gpt4 book ai didi

php - AJAX 和 PHP - 未获取 .responsetext

转载 作者:行者123 更新时间:2023-12-02 20:12:57 27 4
gpt4 key购买 nike

我有一个表单(稍后将添加表单验证),允许用户为房地产网站“添加属性”。这是我的 html/js:

<!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;

?>

我知道这可能是一个小语法错误,这是我通常不会问的问题,但我已经花了 2 个多小时在它上面,并且感到非常沮丧。谢谢。

最佳答案

更新:varrent = document.getElementById('rent').value; 抛出错误,因为没有 id 为“rent”的元素。

旧:您没有取消提交按钮的默认操作。从 addProperty 方法返回 false,然后您应该看到响应。

关于php - AJAX 和 PHP - 未获取 .responsetext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6734445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com