[英]Searchbox (AJAX) won't load requested data
so I have a problem I can't find the error.所以我有一个问题,我找不到错误。
By default the page should load all data and when I hit search, only the requested ones (no refreshing page).默认情况下,页面应该加载所有数据,当我点击搜索时,只有请求的数据(没有刷新页面)。
(Even better would be if I could also change the outcome/request by changing the url without having to type in in the input field: (如果我也可以通过更改 url 而无需在输入字段中输入来更改结果/请求,那就更好了:
entering url: .../searchpage.php?search=banana --> results for banana输入 url: .../searchpage.php?search=banana --> 香蕉的结果
entering url: .../searchpage.php?search=apple --> results for apple输入 url: .../searchpage.php?search=apple --> 苹果的结果
but small steps first.)但首先是小步骤。)
Do you have maybe see where my problem is?你有没有可能看到我的问题在哪里? Or so you know some good pages where I can find solutions/information for my problem?
或者你知道一些好的页面,我可以在其中找到我的问题的解决方案/信息? \
\
A big thankyou in advance!提前非常感谢您!
index.php:索引.php:
<script src="assets/js/jquery.min.js"></script> //v3.4.1
<section class="wrapper">
<div class="formpost">
<div class="searchpannel">
<input type="text" class="searchBox" name="searchBox" id="searchBox" placeholder="Search..">
<button type="submit" id="searchBtn">SEARCH</button>
</div>
<div id="SearchResult"> <?php include 'startdata.php'?> </div>
</div>
</section>
<script>
$(document).ready(function(){
$('#searchdata').click(function(e){
e.preventDefault();
var searchtext = $('input[name=searchBox]').val();
$.ajax({
type: "POST",
url: "fetchdata.php",
data: {
"search_post_btn": 1,
"searchBox": searchBox,
},
dataType: "text",
success: function (response) {
$("#SearchResult").html(response);
}
})
})
})
</script>
fetchdata.php: fetchdata.php:
<?php
$conn = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($conn, 'ajax');
if(isset($_POST['search_post_btn'])) {
$search = $_POST['searchBox'];
$query = "SELECT * FROM ajaxtest WHERE name LIKE '%$search%' ";
$query_run = mysqli_query($conn,$query);
if(mysqli_num_rows($query_run) > 0){
WHILE ($row = mysqli_fetch_assoc($query_run)) {
echo "<h2>Hallo, my name is ";
echo $row['name'];
echo "<strong>";
echo $row['famname'];
echo "</strong></h2><p> On the list I'm place ";
echo $row['id'];
echo "</p>";
}
}
}
?>
Hi you have to do some modification in your code嗨,您必须对代码进行一些修改
$.ajax({
type: "POST",
url: "fetchdata.php",
data: {
"search_post_btn": 1,
"searchBox": searchBox,
},
dataType: "json",
success: function (response) {
$("#SearchResult").html(response);
}
})
----- PHP CODE ---- ----- PHP 代码----
<?php
$conn = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($conn, 'ajax');
if(isset($_POST['search_post_btn'])) {
$search = $_POST['searchBox'];
$query = "SELECT * FROM ajaxtest WHERE name LIKE '%$search%' ";
$query_run = mysqli_query($conn,$query);
if(mysqli_num_rows($query_run) > 0){
$design = "";
WHILE ($row = mysqli_fetch_assoc($query_run)) {
$design .= "<h2>Hallo, my name is ".$row['name']."<strong>".$row['famname']."</strong></h2><p> On the list I'm place ".$row['id']."</p>";
}
print_r(json_encode($design));
die;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.