gpt4 book ai didi

javascript - PHP/jQuery 即时搜索不工作

转载 作者:行者123 更新时间:2023-12-03 03:05:16 24 4
gpt4 key购买 nike

我正在尝试一个教程来学习如何使用 PHP/jQuery 进行即时搜索。我似乎无法找到为什么这段代码不起作用。当我将 PHP 与索引放在同一个文件中时,此搜索之前可以工作,但是当我将其移动到另一个文件时,它停止工作。每次击键时我都会收到此控制台错误消息:ReferenceError:找不到变量:$_POST。任何帮助将不胜感激。

index.php 文件

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset-utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function searchkey() {
var searchTxt = $("input[name='search']").val();
$_POST("search.php", {searchVal: searchTxt}, function(output) {
$("#output").html(output);
});
}
</script>
<title>Search</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="Search for members..." onkeyup="searchkey();">
<input type="submit" value="Search">
</form>
<div id="output"></div>
</body>
</html>

search.php 文件(与index.php 位于同一位置)

<?php
$connection = mysqli_connect('localhost','root','root','LBD');
$output='';

if(isset($_POST['searchVal'])){
$searchkey= $_POST['searchVal'];
$searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);

$query = mysqli_query($connection,"SELECT * FROM members WHERE ownerName LIKE '%$searchkey%' OR companyName LIKE '%$searchkey%'") or die("Could not search!");
$count = mysqli_num_rows($query);

if($count == 0){
$output="There was no search result!";
}
else{
while($row=mysqli_fetch_array($query)){
$oName=$row['ownerName'];
$cName=$row['companyName'];

$output .='<div>'.$oName.'<br/>'.$cName.'</div>';
}
}
}
echo ($output);
?>

最佳答案

看起来您在脚本中使用了 PHP $_POST..

尝试使用:

$.POST

关于javascript - PHP/jQuery 即时搜索不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193390/

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