Ajax在字符串上返回undefined - 表单自动填充

时间:2018-02-21 10:59:50

标签: javascript php ajax

我有一个表单,我想自动填充,具体取决于数据库中的值。

因此,当用户在表单中输入他们的电子邮件地址时,它会自动提取该人的电话和姓名。

当我输入一个数字作为电子邮件时似乎有效。但是,当我以电子邮件的形式输入电子邮件时,它会返回undefined。

这是我的代码

 <script>
var ajax = getHTTPObject();

function getHTTPObject()
{
    var xmlhttp;
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      //alert("Your browser does not support XMLHTTP!");
    }
    return xmlhttp;
}

function updateCityState()
{
    if (ajax)
    {
        var email = document.getElementById("guest_email").value;

        if(email)
        {

          var url = "test04.php";
            var param = "?guest_email=" + email;


            ajax.open("GET", url + param, true);
            ajax.onreadystatechange = handleAjax;
            ajax.send(null);

        }
    }
}
function handleAjax()                                                                                                                           
{
    if (ajax.readyState == 4)
    {
        citystatearr = ajax.responseText.split(",");

        var gname = document.getElementById('guest_name');
        var gtel = document.getElementById('guest_tel');

        gname.value = citystatearr[0];
        gtel.value = citystatearr[1];
    }
}
</script>

修改

<tr>
    <td>Email:</label></td>
    <td><input type='text' name='guest_email' id="guest_email" onChange="updateCityState()"></td>
</tr>
<tr>
    <td>Name:</td>
    <td><input type='text' id='guest_name' /></td>
</tr>
<tr>
    <td>Tel:</td>
    <td><input type='text' id='guest_tel'></td>
</tr>

test.04.php

<?php

$conn = mysqli_connect("","","","");

$result = mysqli_query($conn,"SELECT guest_name, guest_tel FROM guest WHERE guest_email=".mysqli_real_escape_string($conn, $_GET['guest_email']))  or die(mysqli_error());

$row = mysqli_fetch_array($result);
echo $row['guest_name'].",".$row['guest_tel'];

?>

1 个答案:

答案 0 :(得分:0)

可能会有帮助....以JSON格式发送数据(将逗号分隔的响应更改为JSON)... 这只是为了测试...... 您还需要在JS代码中更改它...... 这只是代码改进

$email = mysqli_real_escape_string($conn, $_GET['guest_email']);
$query = "SELECT guest_name, guest_tel FROM guest WHERE 
guest_email='".$email."' LIMIT 1";
 $result = mysqli_query($conn, $query) or die(mysql_error());
$response = array();
if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $response['guest_name'] = ($row['guest_name'] != '') ? $row['guest_name'] : '';
        $response['guest_tel'] = ($row['guest_tel'] != '') ? $row['guest_tel'] : '';
    }
}
echo  json_encode($response, true);  

JS CODE

function handleAjax()                                                                                                                           
{
  if (ajax.readyState == 4)
   {
    var gname = document.getElementById('guest_name');
    var gtel = document.getElementById('guest_tel');
    if(!!ajax.responseText) {
      var result = JSON.parse(ajax.responseText);
      if(!!result){
        gname.value = (!!result.guest_name) ? result.guest_name : '';
        gtel.value = (!!result.guest_tel) ? result.guest_tel : '';
      } 
    }
   }
 }