2010-06-04 64 views
0

我在Firebug中看不到任何錯誤,我無法弄清楚AJAX有什麼問題。我的腳本有什麼問題?

PHP:

<?php 


$subject = $_POST['subject']; 

echo $subject; 



?> 

的Javascript

<html> 
<head> 
<script type="text/javascript"> 
/* <![CDATA[ */ 

    function ajax() { 


     if (window.XMLHttpRequest) { 
      xmlhttp=new XMLHttpRequest();   
     } else { 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     xmlhttp.onreadystatechange=function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
       document.getElementById("result_div").value = xmlhttp.responseText; 
      } 
     } 
     xmlhttp.open("POST","message.php",true); 
     xmlhttp.send(); 

     return false; 
    } 

    /* ]]> */ 
</script> 
</head> 
<body> 
<div id="result_div"></div> 
<form action="" onsubmit="return ajax();"> 

    <select name="teest"> 
     <option value="hi">hi</option> 
    </select><br /> 

    <input type="text" name="subject"> <br /> 
      <input type="submit"> 

</form> 

</body> 
</html> 
+2

意見建議:jQuery – kamasheto 2010-06-04 23:15:35

+0

更具體地說,jQuery序列化將爲您構建提交數據(http://api.jquery.com/serialize/),並使得提交請求變得更容易(http:///api.jquery.com/jQuery.post/) - 就像$ .post(「message.php」,$(「#testform」)。serialize());如果你的表單的ID是「testForm」。留給你添加設置div值。 – 2010-06-04 23:33:00

回答

2

你是不是POST荷蘭國際集團任何東西。當使用阿賈克斯表格而不是爲您自動處理。

+0

使用get或post更理想嗎? – Strawberry 2010-06-04 23:41:33

+0

因爲xmlhttp.send();是空的。 xmlhttp.send( '受試者=你好');應該將div值設置爲hello。 – 2010-06-04 23:42:00

+1

獲取vs發佈後:http://stackoverflow.com/questions/715335/get-vs-post-in-ajax – 2010-06-04 23:44:27