2013-05-26 119 views
1

我使用下面的AJAX腳本傳遞變量到PHP腳本:獲得價值傳遞的變量在URL中的AJAX腳本

<script> 
    function showUser(str) 
    { 
     if (str == "") 
     { 
      document.getElementById("txtHint").innerHTML = ""; 
      return; 
     } 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      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("txtHint").innerHTML = xmlhttp.responseText; 
      } 
     } 

     xmlhttp.open("GET", "/scripts/notes.php?s=2&q=" + str, true); 
     xmlhttp.send(); 
    } 
</script> 

在可變的「s」剛剛成立的那一刻2,但我想在選擇動態設置此使用的電流值在頁面上:

<select id='sessions'> 
<option value='5'>5 sessions</option> 
<option value='10'>10 sessions</option> 
<option value='1000'>All sessions</option> 
</select> 

我已經嘗試使用:

var sessions= $("#sessions").val(); 

在設置URL之前在上面的腳本中,然後將's = 2'更改爲's = sessions',但正確的值沒有被傳遞(我懷疑s被傳遞爲空)。

我需要更改以使其工作?

回答

1

改變的= 2「到 'S =會話'

設置你的字符串"/scripts/notes.php?s=session&q="不會傳遞變量。嘗試"/scripts/notes.php?s=" + session + "&q="

function showUser(str) 
{ 
    var sessions= $("#sessions").val(); 

    if (str == "") 
    { 
     document.getElementById("txtHint").innerHTML = ""; 
     return; 
    } 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     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("txtHint").innerHTML = xmlhttp.responseText; 
     } 
    } 

    xmlhttp.open("GET", "/scripts/notes.php?s="+sessions+"&q=" + str, true); 
    xmlhttp.send(); 
} 
+0

謝謝,修復了它。 – Nick