使用xmlhttp.send传递多个值

时间:2018-04-13 04:56:54

标签: javascript xmlhttprequest

我试图将两个不同的值传递给XMLHttpRequestObject.send,但似乎无法让php脚本同时读取它们,它只会读取传递的第一个值。如何让 PHP 读取这两个值,下面是示例代码:

HTML

  <select id="mes" onchange="callCourseYards(this);">
  <option>-</option>
  </select>

的Javascript

  var XMLHttpRequestObject = false;

    if(window.XMLHttpRequest){
    XMLHttpRequestObject = new XMLHttpRequest();
    }else if(window.ActiveXObject){
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

  function callCourseYards(selectObject){

        if(XMLHttpRequestObject){

        XMLHttpRequestObject.open("POST", "php/CourseChoiceTeeOff.php");

        XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        XMLHttpRequestObject.onreadystatechange = function(){
            if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
                var returnData = XMLHttpRequestObject.responseText;

                var messageDiv = document.getElementById('tee');
                messageDiv.innerHTML = returnData;

            }   
        }

        var data = selectObject.value;
        var course = selectObject.value;
        XMLHttpRequestObject.send("data=" + data + "course=" + course);

    }
    return false;
}

PHP

<?php 
    $pdo_dsn='localhost';
    $dbname ='XXX';
    $mysql_user='XXX';
    $mysql_pass='XXX';

    $db = mysqli_connect($pdo_dsn, $mysql_user, $mysql_pass, $dbname);

    if(!$db){
        print "Unable to connect to MySQL";
    }

    $myData = $_POST['data'];
    $myData1 = $_POST['course'];

    $sql_state = "SELECT * FROM ".$myData1." WHERE CourseCode = '".$myData."' ORDER BY CourseCode";

    $result = mysqli_query($db, $sql_state);

    $out = "";
    $myrowcount = 0;

    if(!$result){
        $out = "Error";
    }else{

        $out = "<option>Selecciona un campo</option><br/ >";
        $numresults = mysqli_num_rows($result);
        for ($i = 0; $i < $numresults; $i++){
            $row = mysqli_fetch_array($result);
            $teeOff = $row['TeeOff'];
            $out .= "<option id='".$myData."' value='".$teeOff."'> ".$teeOff."</option>";
        } 
        $out .= "<br/ >";
    }

    print $out; 
?>

我似乎无法通过XMLHttpRequestObject获取值进入 PHP 。我不确定它的语法或它们应该如何进入 PHP

我尝试过使用&amp;以及用于连接两个值的+符号,但我不确定这是否可行。有谁知道如何解决这个问题?我需要两个值才能在数据库中执行 mySQL 搜索。

非常感谢任何帮助。感谢

0 个答案:

没有答案