【发布时间】:2018-08-20 04:10:13
【问题描述】:
在此处输入图像描述我是 php 和 JavaScript/jQuery 的新手。在开始学习 php 之前,我对 htmL 和 css 有点了解。
我一直在编辑我刚刚下载的模板并遇到了这个问题,我一直试图找出解决方法
我的问题是我无法在单击结果时将搜索框的结果重定向到我的 update.php 页面,并在找不到匹配项时清除搜索,这就是我想要完成的输出。
当用户单击名称时,我无法从表格的锚点执行相同的操作,对我的搜索栏执行相同的操作。
我希望有人可以帮助我。
(顺便说一句,由于某些视觉障碍,我是放大镜和屏幕阅读器用户。)
提前致谢:-)
<?php
$link = mysqli_connect("localhost", "root", "", "ajax_crud");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if(isset($_REQUEST['term'])){
$search_string = "SELECT * FROM name WHERE name LIKE ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_term);
$param_term = $_REQUEST['term'] . '%';
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<p>" . $row["name"] . "</p>";
}
} else{
echo "<p>No matches found!</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " .
mysqli_error($link);
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
?>
/* and the script
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}enter image description here
});
$(document).on("click", ".result p", function(){
$(this).parents(".search-
box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
/* the part of the table where I can redirect the user to the update.php
fiLe
echo sprintf('<td><a href="update.php?id=%s">%s</a></td>', $row['id'],
$row['name']);
this is how my index page Looks Like
this is an exampLe of a search in action
i cLicked on a resuLt from the search
the resuLt is pLaced in the searchbox on cLick
this is the update.php wherein the user shouLd be redirected upon cLicking the resuLt
很抱歉,我无法更早地添加这些代码行。 我希望这可以澄清事情并使我自己更容易理解。
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
【问题讨论】:
-
你的问题对我来说不是很清楚。我试图从您给出的内容中了解您的 HTML 的形状,并认为您有一个要搜索的文本字段,当用户键入内容时,您将从 PHP 脚本中获取结果并填充
.result下拉列表。你能添加.result所在页面的HTML和backend-search.php的输出(它应该是<option>标签的集合)。另外,我不确定您的意思是 result from my searchbox redirect to my update.php page 请提供更多信息(更多代码、屏幕截图等) -
我想要实现的是将用户重定向到 update.php 页面,并从搜索框中获得相应的结果,就像图片上的一样。我评论了可以重定向用户的代码通过单击 tabLe 中的名称来更新.php 页面(即 echo sprintf('
%s ', $ row['id'], $row['name']); ) 我的问题是当我单击搜索结果时,我无法弄清楚如何在我的 jquery 脚本中放置相同的锚标记,而不是结果只会出现在搜索框中..(我正在尝试添加照片) -
我刚刚添加了关于我的问题的照片..
标签: javascript php jquery