gpt4 book ai didi

php - 实时 AJAX 搜索手机号码

转载 作者:行者123 更新时间:2023-11-29 12:13:53 25 4
gpt4 key购买 nike

我正在制作一个网络应用程序。主索引页有一个用于搜索的大输入标签。一个人输入手机号码,然后通过 AJAX 和 php,我对 MySQL 数据库进行实时搜索。

我能够从数据库中检索所需的行。我尝试对结果数组执行 print_r() ,结果出现了。但是当我尝试仅使用数组的一个元素时,却没有。

请看看是否能找到问题:

Javascript

<script>
$(document).ready(function(e) {
$('#search').keyup(function(e) {
if($(this).val().length > 2){
$.get('php/search.php', {input: $(this).val()}, function(data){
alert(data);
});
}
});
});
</script>

php/search.php 文件是:

<?php
require_once 'customer.php';

$input = $_REQUEST['input'];
print_r($cus->read($input));
?>

customer.php 文件是:

public function read($input) {
//What search needs to do
//Part 1 - Check if input is a mobile number or Text
if(is_numeric($input) && strlen($input) > 2){
//Search in mobile number
$sql = "SELECT * FROM customers WHERE mobile LIKE '{$input}%' LIMIT 1";
$result = $this->query($sql);
$output = json_encode(mysqli_fetch_assoc($result));
return $output;
}else{}
}

$cus = new Customer();

When it works, this is what i can see:

如果我删除 search.php 中的 print_r 函数并在 javascript 中更改

alert(data);

alert(data[0]);

我收到一条警告,上面写着“未定义”。 enter image description here

奇怪的是,如果我更改为

alert(data[id]);

什么也没发生。但如果我改为

alert(data[name]);

我又变得未定义了。

最佳答案

试试这个

$(function() {
$('#search').keyup(function(e) {
var val = $(this).val();
if (val.length > 2) {
$.get('php/search.php', {
input: val
}, function(data) {
var res = $.parseJSON(data);
if (res) alert(res["name"]);
});
}
});
});

关于php - 实时 AJAX 搜索手机号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30190658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com