drm16022 2011-07-18 14:26
浏览 24
已采纳

AJAX和PHP - 没有获得.responsetext

I have a form (will add form validation later) that allows the user to 'Add a Property' for a real estate site. Here's my 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>

Here's my 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;

?>

I know it's probably a minor syntax error, which is something I usually wouldn't ask about on SO but I've spent 2+ hours on it and am overly frustrated. Thanks.

  • 写回答

2条回答 默认 最新

  • dongshuohuan5291 2011-07-18 14:30
    关注

    UPDATE: var rent = document.getElementById('rent').value; threw error because there was no element with id "rent".

    OLD: You are not cancelling the default action of the submit button. Return false from addProperty method and then you should see the response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制