weixin_33690963 2019-06-02 17:56 采纳率: 0%
浏览 32

AJAX使用PHP自动填充

I am trying to populate two fields 'dep' and 'arr' when field 'flightnumber' is written into and 'onpointermove'.

Here is the form code:

<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;">

Here is the 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>   

And here is the php, which i have tested and does return correct results...

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


?>

I can only assume that the issue here is within the AJAX coding, as the PHP as mentioned is working.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办