JS函数将空字符串传递回php,但是有一个值?

时间:2015-03-05 17:27:51

标签: javascript php

所以我试图将几个值传递回这样的PHP页面。

function showAccountInfo(obj){

    var value = obj.value;
    var content = obj.querySelector("option:checked").textContent;

    alert("value: " + value + " content: " + content);

    if(obj == ""){
        return;
    }
    else {
        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("facilities").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getinfo.php?=q"+value+"&c="+content, true);
        xmlhttp.send(); 
    }
}

当警报响起时,它会显示每个变量的正确值。但是,当我将该值传递给getinfo.php并使用变量value来完成查询并将其回显到页面时,它会将其显示为空。

$q = ($_GET['q']);

$sql = "SELECT *, account.account_name FROM facility "
 . "INNER JOIN account ON account.account_id = facility.account_id "
 . "WHERE facility.account_id = '".$q."'";

echo $sql;

结果:SELECT *, account.account_name FROM facility INNER JOIN account ON account.account_id = facility.account_id WHERE facility.account_id = ''

在我之前question之前,我的一切运作正常。

1 个答案:

答案 0 :(得分:1)

您的网址格式不正确

更改

xmlhttp.open("GET","getinfo.php?=q"+value+"&c="+content, true);
                                ^

xmlhttp.open("GET","getinfo.php?q="+value+"&c="+content, true);
                                 ^

注意更改第一个=