如何使用XMLHttpRequest获取参数值?

时间:2016-02-18 07:36:19

标签: javascript php xml

我在JavaScript中定义了以下函数,用于调用另一个名为database_value.php的文件,我在其上传递Var str

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();
}

如何在文件str上获取database_value.php的值?

1 个答案:

答案 0 :(得分: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;
    }
?>