【问题标题】:How do I get a parameter value using XMLHttpRequest?如何使用 XMLHttpRequest 获取参数值?
【发布时间】:2016-05-30 06:20:18
【问题描述】:

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

如何获取文件database_value.phpstr 的值?

【问题讨论】:

  • 非常感谢@chetan

标签: javascript php xml


【解决方案1】:

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;
    }
?>

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多