2016-02-18 86 views
2

我已經定義在JavaScript下面的函數調用名爲database_value.php另一個文件上我傳遞Var str如何使用XMLHttpRequest獲取參數值?

function subFunction(str){ 
    if (str=="") { 
     document.getElementById("sci").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("sci").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","database_value.php?q="+str,true); 
    xmlhttp.send(); 
} 

我怎樣才能獲取有關文件database_value.phpstr價值?

+1

嘗試'$ _GET [ 'Q']' –

+0

非常感謝@chetan – Aurea

回答

0

database_value.php

<?php 
    if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['q'])){ 

     /* use the value from the GET array */ 
     $q=$_GET['q']; 

     /* or better with some filtering for user supplied values */  
     $q=filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING); 

     /* use the param in your query */ 

     /* echo results from db query or just the value as a test */ 
     echo $q; 
    } 
?> 
相關問題