2015-03-31 50 views
0

良好的一天,這是我的索引PHP創建選擇標籤使用Ajax

<!DOCTYPE html> 
<html> 
<body> 

<script> 
    function show_month(var) { 
     if (windows.XMLHttpRequest) { 
      xmlhttp = new XMLHttpRequest(); 
     } 
     else { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.open("GET","month.php?q="+var,true); 
     xmlhttp.send(); 
    } 
</script> 

<?php 
    $from = (date('Y')); 
    $to = 2050; 
    echo '<form>'; 
    echo '<select name="year" onchange="show_month(this.value)">'; 

    for($y = $from; $y <= $to; $y++) { 
     echo "<option value=$y>{$y}</option>"; 
    } 
    echo '</select>'; 
    echo '<form>'; 
?> 

<div id="txtHint"><b>here will be info</b></div> 

</body> 
</html> 

的代碼,這裏是我的month.php

的代碼正如你看到的,我創建選擇標籤用PHP等等我可以通過使用循環做出多個選項,並且我希望如果我選擇2015年javascript應該打印消息實際年,但它不工作我認爲這個問題是在某處選擇或發送價值可以有人比我更聰明看看這段代碼並告訴我什麼是錯的?

回答

0

你的js代碼沒有響應代碼。你的代碼中還有一些其他錯誤會導致你的問題。

show_month(var)你不能把var作爲這個特定的JS,改成別的東西。

windows.XMLHttpRequest這是窗口不是windows。

爲了顯示您需要使用的東西; alert(xmlhttp.responseText); 如果你的迴應。

<script> 
    function show_month(t) { 
     if (window.XMLHttpRequest) { 
      xmlhttp = new XMLHttpRequest(); 
     } 
     else { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
      } 
     } 

     xmlhttp.open("GET","month.php?q="+t,true); 
     xmlhttp.send(); 
    } 
</script> 

最後你不需要在你的month.php <!DOCTYPE html>如果你是剛剛返回文本(最有可能的,你不需要它的一個原因)。

+0

我不想警覺我只是想要文本「在這裏將信息「將更改爲文本行在month.php – palci12 2015-03-31 09:52:16

+0

啊好吧,然後只需添加; document.getElementById(」txtHint「)。innerHTML = xmlhttp.responseText;而不是警報 – Dan 2015-03-31 09:54:08

+0

我添加,但沒有改變 – palci12 2015-03-31 09:56:12

0

你的js代碼缺少代碼來處理服務器響應

<script> 
function show_month(var) { 
    if (windows.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } 
    else { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
     document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","month.php?q="+var,true); 
    xmlhttp.send(); 
} 

+0

我添加了處理響應,但它沒有幫助仍然我的消息不會改變:( – palci12 2015-03-31 09:48:28

+0

你回答是最有用的,我只是使用小寫innerHTML現在它的工作就像我想thx – palci12 2015-03-31 09:57:41