从Select into xmlhttp传递数组

时间:2017-03-10 16:12:36

标签: javascript php

我想将select中的值数组传递给下面的xmlhttp.open请求,以便我可以解析新页面中的变量。怎么能实现这一目标?我尝试了下面的语法,但是当按下按钮时它会抛出500错误。

    <script type="text/javascript">
function GetData() {
    var xmlhttp = new XMLHttpRequest();
    var hiredate = document.getElementById("hiredate").value; 
    var termdate = document.getElementById("termdate").value
    var arr = document.getElementById("bq").value;
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
           if (xmlhttp.status == 200) {
               document.getElementById("data").innerHTML = xmlhttp.responseText;
           }
           else if (xmlhttp.status == 400) {
              alert('There was an error 400');
           }
           else {
               alert('something else other than 200 was returned');
           }
        }
    };
    xmlhttp.open("GET", "Query.php?hiredate="+hiredate+"&termdate="+termdate+"&array="+arr, true);
    xmlhttp.send();
}
</script>

<select name="bq" size="12" multiple="multiple" tabindex="1">
<option value="HireDate">HireDate</option>
<option value="TermDate">TermDate</option>
<option value="Dept">Dept</option>
<option value="Manager">Manager</option></select>
Start Date: <input type="date" name="hiredate" id="hiredate">
End Date: <input type="date" name="termdate" id="termdate">
<input type="submit" value="Submit" id="ajaxButton" onclick="GetData()">
<div id="data">

修改

这是我的Query.php - 我省略了与服务器的实际连接,因为我知道这是合理的

<?php

    $hiredate = $_GET['hiredate'];
    $termdate = $_GET['termdate'];
    $sql = '';
    $selected_fields = array();   
    foreach ($_GET['array'] as $selectedOption){
        $selected_fields[] = $selectedOption;
    }
    if(!empty($selected_fields)){
        $fields = implode(',', $selected_fields);
        $sql = 'SELECT '.$fields.' from testtable'; 
    }

    $sql = $sql & "WHERE hiredate = $hiredate AND termdate = $termdate";
    echo $sql
?>

0 个答案:

没有答案