gpt4 book ai didi

php - 使用 AJAX 完成自动预测 - 不起作用

转载 作者:行者123 更新时间:2023-11-28 08:56:59 25 4
gpt4 key购买 nike

AJAX JS 功能存在问题,该功能将从 PHP 数据库获取信息并在用户完成之前预测搜索词。

/js/primary.js:

$(document).ready(function() {
$('.autosuggest').keyup(function() {

var search_term = $(this).attr('value');
//alert(search_term); //takes what is typed in the input and alerts it
$.post('ajax/search.php', {search_term:search_term}, function (data) {
alert('Ajax Activated');
$('.result').html(data);
$('.result li').click(function() {
var result_value = $(this).text();
var id_result = $(this).data("id");
$('.autosuggest').attr('value', result_value);
$('.id').attr('value', id_result);
$('.result').html('');
});
});
});
});

/ajax/search.php:

            require_once '../Configuration.php';
if (isset($_POST['search_term']) == true && empty ($_POST['search_term']) == false) {

$search_term = mysqli_real_escape_string($sql_connection, $_POST['search_term']);


$query = mysqli_query($sql_connection,"SELECT `id`,`customer_name`,`postcode` FROM `Customers` WHERE `customer_name` LIKE '$search_term%' ");

while(($row = mysqli_fetch_assoc($query)) !== false) {
//loop
echo '<li data-id="',$row['id'],'" >',$row['customer_name'],' ',$row['postcode'],'</li>';

}
}


?>

Selectcustomer.php:

                        $customer_id = (int) $_GET['id'];

$query = "SELECT * FROM Customers WHERE id = {$customer_id}";

$result = mysqli_query($sql_connection, $query) or die('<p>' . $query . '</p><div>' .
mysqli_error($sql_connection) . '</div>');

$customer = mysqli_fetch_assoc($result);

?>
<h2>Quick find users</h2>
<div class="block ">

<form action="Customer.php" method="post">


<input type="text" class="autosuggest"> <input type="submit" value="Go Go Go!">
<div class="dropdown">
<ul class="result">
</ul>
</div>
<input type="hidden" name="id" class="id" value="">
</form>




</div>

问题:该字段中没有出现任何内容(这曾经有效)。

我修复了所有不起作用的路径。但它仍然不提示搜索。

最佳答案

我被指示查看包含无限循环的 Search.php:

        while(($row = mysqli_fetch_assoc($query)) !== false) {
//loop
echo '<li data-id="',$row['id'],'" >',$row['customer_name'],' ',$row['postcode'],'</li>';

更改为:

        while($row = mysqli_fetch_assoc($query)) {
//loop
echo '<li data-id="',$row['id'],'" >',$row['customer_name'],' ',$row['postcode'],'</li>';

关于php - 使用 AJAX 完成自动预测 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18336173/

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