gpt4 book ai didi

php - 使用自动完成功能输入文本 - 未捕获的 TypeError : $. ajax 不是 PHP、MySQL 和 jQuery 中的函数

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

我正在尝试使用 PHP 和 MySQL 中数据库中的用户信息在 jQuery 中进行自动完成文本输入。我收到以下错误:

Uncaught TypeError: $.ajax is not a function
at HTMLInputElement.<anonymous> (file.php:332)
at HTMLInputElement.dispatch (jquery-3.3.1.slim.min.js:2)
at HTMLInputElement.v.handle (jquery-3.3.1.slim.min.js:2)

我在所有页面中包含的 header.php 上导入 jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

这是我的 file.php,它是包含脚本和输入的文件,我将只保留部分代码,当然输入位于表单内

<input type="text" name="autor" id="autor" placeholder="Escreve o nome do autor" />
<div id="autorLista"></div>

<script>
$(document).ready(function() {
$('#autor').keyup(function() {
var query = $(this).val();;
console.log(query);

if (query != '') {
$.ajax({
url: "./components/search.php",
method: "POST",
data: {
query: query
},
success: function(data) {
$('#autorLista').fadeIn();
$('#autorLista').html(data);
}
});
}
});

$(document).on('click', 'li', function() {
$('#autor').val($(this).text());
$('#autorLista').fadeOut();
});
});
</script>

我什至不知道 php 代码是否正常工作,但我认为是的。我将其保留在下面(search.php):

<?php

if (isset($_POST['query'])) {
$link = new_db_connection();
$stmt = mysqli_stmt_init($link);

$output = '';

$query = "SELECT id_user, nome_user FROM users WHERE nome_user LIKE ?";

if (mysqli_stmt_prepare($stmt, $query)) {
$output = "<ul>";

if (mysqli_stmt_num_rows($stmt) > 0) {
mysqli_stmt_bind_param($stmt, 's', $nome_user);

mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $id_user, $nome_user);

if (mysqli_stmt_fetch($stmt)) {
$output .= "<li>" . $nome_user . "</li>";
}
} else {
$output = '<li>O utilizador que procura não existe.</li>';
}
$output .= "</ul>";
echo $output;
} else {
echo 'erro';
}
}

我尝试过更改 jquery 的版本,检查了 slim 版本等,但没有任何效果......请问有什么想法吗?如有任何疑问或不清楚,请告诉我。谢谢!

最佳答案

在您的错误中,它显示您正在使用 jQuery slim,它没有 Ajax。

(jquery-3.3.1.slim.min.js:2)

请检查您是否仅使用普通 jQuery,而不是精简版本。

关于php - 使用自动完成功能输入文本 - 未捕获的 TypeError : $. ajax 不是 PHP、MySQL 和 jQuery 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616585/

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