gpt4 book ai didi

php - 如何使用 ajax jquery、php 和 mysql 进行 auto_search

转载 作者:行者123 更新时间:2023-11-29 06:07:52 24 4
gpt4 key购买 nike

我已经使用 Javascript AJAX 创建了自动建议,现在我想使用 Jquery AJAX 创建自动建议,这里是我使用 jquery AJAX 编写的代码

这是我的indexa.php

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$("#search_input").keyup(function(){
var txt = $("#search_input").val();
$.get("search.inc.php?search_username="+document.search_form.txt, {suggest: txt}, function(result){
$("searchResults").html(result);
});
});
});
</script>

</head>
<body>

<form id="search" name="search_form" action="<?php $_SERVER['PHP_SELF']; ?> " method="post">

Search For Names : <input type="text" id="search_input" name="search_text"><br/><br/>
<!-- <input type="submit" > -->

</form>

<div id="searchResults"></div>
</body>
</html>

这是我的 search.inc.php

  <?php

if (isset($_GET['searched_username'])) {
$username = $_GET['searched_username'];
}

if (!empty($username)) {
if (@$db_connect=mysqli_connect('localhost', 'root', '')) {
if (@mysqli_select_db($db_connect,'my_databse')) {

$query_like = "SELECT `user_name` FROM `members_data` WHERE `user_name` LIKE '%". mysqli_real_escape_string($db_connect,$username)."%'";
$query_like_run = mysqli_query($db_connect,$query_like);
$query_num_rows = mysqli_num_rows($query_like_run);
if ($query_num_rows == NULL) {
echo 'No Result Found';
} else {
if ($query_num_rows == 1) {
echo $query_num_rows ." Result found<br/><br/>";
} else {
echo $query_num_rows ." Results found<br/><br/>";
}
foreach ($query_like_run as $searched_members) {
$searched_results = '<a href="anotherpage.php?searched_username='.$searched_members['user_name'].'">'.$searched_members['user_name']."</a><br/>";
echo $searched_results;
}
}
}
}
}
?>

我在做什么 worng 。请帮助我

最佳答案

从 url 中删除值并将键更改为 search_username

执行以下操作:

 <script type="text/javascript">

$(document).ready(function(){
$("#search_input").keyup(function(){
var txt = $(this).val();
$.get("search.inc.php", {searched_username: txt}, function(result){
$("#searchResults").html(result);//don't forget the # for the id
});
});
});
</script>

关于php - 如何使用 ajax jquery、php 和 mysql 进行 auto_search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235604/

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