【问题标题】:how to fetch mysql data using ajax when one input is filled填充一个输入时如何使用ajax获取mysql数据
【发布时间】:2012-10-04 16:36:56
【问题描述】:

html

<input type="text" name="PName" />
<input type="text" name="PAddress" />
<input type="text" name="PCity" />

mysql表

 ________________________________________________________
| id  |PName          |  PAddress           |  PCity    |
| ----|-------------------------------------------------|
|  1  | John           |  po box no xyz      |  Florida |
?????????????????????????????????????????????????????????

现在我的问题是当我输入姓名时如何使用 ajax 来获取地址和城市?任何帮助表示赞赏

【问题讨论】:

  • 您需要了解更多关于 AJAX、PHP 和数据库的知识。
  • 嗯这不是我的答案? :P ehhe
  • 尝试一下,如果遇到任何问题,请回来。
  • 我使用 fetcher.php 文件连接数据库并获取著名的 sql fetch 数组中的行。
  • 你应该寻找一个处理这个的教程,基本上你需要一个从数据库中检索数据的 php get 函数,并且使用 ajax 你可以使用一个返回此信息的 get 函数。

标签: php javascript sql ajax


【解决方案1】:

不久前,我对我的项目进行了“实时”搜索,所以这里有一些代码根据您的需要进行了修改(我假设您的页面上有 jQuery)。

首先我建议你给你的输入一些id:

<input type="text" name="PName" id="PName" />
<input type="text" name="PAddress" id="PAdress" />
<input type="text" name="PCity" id="PCity" />

之后可以绑定PName字段的keyup事件:

var searchTimeout; //Timer to wait a little before fetching the data
$("#PName").keyup(function() {
    searchKey = this.value;

    clearTimeout(searchTimeout);

    searchTimeout = setTimeout(function() {
        getUsers(searchKey);    
    }, 400); //If the key isn't pressed 400 ms, we fetch the data
});

获取数据的js函数:

function getUsers(searchKey) {
    $.ajax({
        url: 'getUser.php',
        type: 'POST',
        dataType: 'json',
        data: {value: searchKey},
        success: function(data) {
            if(data.status) {
                $("#PAddress").val(data.userData.PAddress);
                $("#PCity").val(data.userData.PCity);
            } else {
                // Some code to run when nothing is found
            }   
        }
    });         
}

当然还有getUser.php 文件:

<?php
    //... mysql connection etc.

    $response = Array();

    $response['status'] = false;

    $query = mysql_query("SELECT `PAddress`, `PCity` FROM `Users` WHERE `PName` LIKE '%".$_POST['value']."%' LIMIT 1"); //Or you can use = instead of LIKE if you need a more strickt search

    if(mysql_num_rows($query)) {
        $userData = mysql_fetch_assoc($query);

        $response['userData'] = $userData;
        $response['status'] = true;            
    }

    echo json_encode($response);

祝你好运! ^^

【讨论】:

  • 为什么我不能让它工作?我使用名为 idCardNo 的字段来获取名称和永久地址。当我转到 getUser.php?A123456(因为我将类型更改为 GET)时,我得到了这个消息。但在我的 html 文件中,我无法得到结果 {"status":true,"userData":{"Name":"Nizar Ibrahim","permAddr":"Dhildhaaruge"}}
  • 如果您使用NamepermAddr,则将$("#PAddress").val(data.userData.PAddress); 更改为$("#PAddress").val(data.userData.permAddr); 并相应地更改另一个。并在SQL 中将$_POST 更改为$_GET。哦,您应该使用getUser.php?value=A123456 直接获取信息。
  • 一点帮助@Arthur pls.. 我无法让它继续工作。我将所有代码上传到intruderexists.0fees.net/index.html数据库img:intruderexists.0fees.net/dbase.png
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 2016-09-22
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多