AJAX使用PHP自动填充

时间:2019-06-02 17:56:41

标签: php html ajax

当试图将字段“ flightnumber”写入“ onpointermove”时,我试图填充两个字段“ dep”和“ arr”。

这是表格代码:

<input type="text" name="flightnumber" id="flightnumber" onpointermove="showUser(this.value)" style="width: 70px;" maxlength="4">
        <input type="text" name="dep" id="dep" style="width: 70px;">
        <input type="text" name="arr" id="arr" style="width: 70px;">

这是AJAX

<script>

function showUser(str) {
  if (str.length=="") {
    document.getElementById("dep").innerHTML="";
    document.getElementById("arr").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 (this.readyState==4 && this.status==200) {
        var myObj = JSON.parse(this.responseText);
        document.getElementById("arr").innerHTML = myObj.dep;
        document.getElementById("dep").innerHTML= myObj.arr;
    }
  }
  xmlhttp.open("GET","getdata.php?q=" + str,true);
  xmlhttp.send();


</script>   

这是我测试过的php,确实返回正确的结果...

<?php
//look up the record based on email and get the firstname and lastname
  $host_name = 'db5000091260.hosting-data.io';
  $database = 'dbs85930';
  $user_name = 'dbu68420';
  $password = '';
  $connect = mysqli_connect($host_name, $user_name, $password, $database);

    $q = $_GET['q'];
    $myObj->dep = "";
    $myObj->arr = "";

    $sql = "SELECT dep, arr FROM flights WHERE flightnumber = {$q}";
    $result = mysqli_query($connect, $sql);

    if (mysqli_num_rows($result) > 0) {

    while($row = mysqli_fetch_assoc($result)) {

    $myObj->dep = $row['dep'];;
    $myObj->arr = $row['arr'];

    $myJSON = json_encode($myObj);

    echo $myJSON;

    }
    } else {
    echo "0 results";
    }


?>

我只能假设这里的问题在AJAX编码内,因为上述PHP可以正常工作。

0 个答案:

没有答案