2011-07-18 16 views
0

私は、ユーザーが不動産サイトに「プロパティを追加する」フォームを持っています(後でフォームの検証を追加します)。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の:ここに私のhtml/jsがあります、私はそれはおそらく、私は通常、SO程度に聞かないと何かあるマイナーな構文エラー、知っている

<?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時間以上が過ぎて欲求不満。ありがとう。

+0

あなたは既にリクエストが本当にあなたの 'add_property.php'スクリプトに到達していることを証明していますか?そうでない場合:あなたが 'add_property.php'の最初の行に' die( 'hard');すれば、Fiddler/Firebug/whatever-http-proxy-toolでどのような応答が表示されますか? –

答えて

2

更新日:var rent = document.getElementById('rent').value; idが "rent"の要素がなかったため、投げられませんでした。

旧:送信ボタンのデフォルト動作をキャンセルしていません。応答falseaddPropertyメソッドから返すと、応答が表示されます。

+0

これは送信ボタンではなく、単なるボタンなので、本質的に本来の動作はありません。私はこれを試しましたが、問題は他の場所にあります。 –

+0

申し訳ありませんが、私の悪い。 PHPとApacheのログをチェックして、サーバー上の何らかのエラーが応答を妨げているかどうか確認しましたか? PHPの出力圧縮を有効にしている場合、通常は 'echo'が動作しません。また、Firebugはサーバーが送信した正確な応答をあなたに伝え、Firefoxを使用している場合はそれを採用します。 – Anshuman

+0

"rent"という名前の要素がなく、したがって 'var rent = document.getElementById( 'rent')。value;という行がエラーをスローするため、JSにエラーが発生している可能性があることに気付きました。 – Anshuman

0

idがrentの要素はありません。 はまた、あなたはencodeURIまたはencodeURIComponentあなたのparams必要があります。

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

URIエンコードは、変数を$ _GETまたは$ _POST varとして使用可能にしますか?それとも別のことですか? –

+0

これは、入力が正しくエンコードされ、 '&a =# 'のような入力文字列がURLクエリを混乱させないことを保証します(たとえば、'# 'の後のすべてがサーバーに送信されません)。 – Saxoier

関連する問題