Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX&;PHP-无法获取.responsetext_Php_Javascript_Ajax - Fatal编程技术网

AJAX&;PHP-无法获取.responsetext

AJAX&;PHP-无法获取.responsetext,php,javascript,ajax,Php,Javascript,Ajax,我有一个表单(稍后将添加表单验证),允许用户为房地产网站“添加属性”。以下是我的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 h

我有一个表单(稍后将添加表单验证),允许用户为房地产网站“添加属性”。以下是我的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>

添加新属性
函数addProperty(){
var name=document.getElementById('name')。值;
var address=document.getElementById('address')。值;
var lat=document.getElementById('lat').value;
var lng=document.getElementById('lng')。值;
var type=document.getElementById('type')。值;
var rent=document.getElementById('rent')。值;
var beddrooms=document.getElementById('beddrooms').value;
var owner_id=document.getElementById('owner_id')。值;
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome和Opera的代码
xmlhttp=新的XMLHttpRequest();
}
否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
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+”&beddrooms=“+beddrooms+”&owner\u id=“+owner\u id;
open(“GET”,url,true);
xmlhttp.send();
}
物业名称:

地址: 拉丁美洲: 液化天然气:
物业类别: 房子 公寓
卧室数量:
所有者Id:

以下是我的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;

?>


更新:
var rent=document.getElementById('rent').value引发错误,因为没有id为“rent”的元素

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

Error: document.getElementById("rent") is null
Line: 39
没有id为
rent
的元素。 您还应该或您的参数:

encodeURIComponent(document.getElementById('name').value)
...

它不是一个提交按钮-它只是一个按钮,所以它本身没有执行任何操作-我尝试了这个,问题出在其他地方。对不起,我的错。您是否检查过PHP和Apache日志,以查看服务器上是否有某种错误阻止响应?如果启用了PHP输出压缩,则通常
echo
不起作用。此外,Firebug会告诉您服务器发送的确切响应,如果您使用Firefox,一定要使用它。注意到您的JS可能会出错,因为没有名为“rent”的元素,因此行
var rent=document.getElementById('rent').value
将抛出错误。您是否已经证明请求确实到达了您的
add\u property.php
脚本?如果不是:如果你
die('hard'),你会在Fiddler/Firebug/你使用的任何http代理工具中看到什么反应
add_property.php
的第一行中,URI编码是否使变量作为$\u GET或$\u POST变量可用?还是其他原因?这只能确保输入正确编码,并且像
&a=#
这样的输入字符串不会“弄乱”URL查询(例如,“#”之后的所有内容都不会发送到服务器)。