当用户在文本框中搜索其获取数据但页面已重新加载时。 我想要当用户搜索数据加载而不重新加载或刷新页面时 我在一页Ajax.php中编写代码。
这是我的代码,无法进行实时搜索 呼叫Ajax并像这样查询
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="jquery-3.4.1.min.js"></script>
js:
$(document).ready(function () {
$('#Search-btn').click(function(){
var searchtxt =$('#search').val();
if($.term(searchtxt)!= ''){
$.ajax({
url="ajax.php",
method:"POST",
data:{search:searchtxt},
success:function(data)
{
$('#search').val();
}
});
}
});
setInterval(function() {
$('#here').load("ajax.php").fadeIn("slow");
}, 1000);
});
</script>
这是我的PHP代码
<form >
<div style="width:100%;height:80px;">
<h3 style="float: left;"> Student Who Have Exames In The Same Day</h3>
<div style="text-align:right;">
<input type="text"id="search" name="search" style="height:25px;border-radius: 25px;margin:40px;">
<input type="submit"class="hide" id="Search-btn"name="btn"value="Search">
</div>
</form>
<div id="here"style="width:100%;height:80px;border:3px solid red;" >
<?php
$connection = mysqli_connect("localhost", "FEP", "FEP", "FEP", '8889');
$test='CREATE TEMPORARY table search
SELECT `Class_ID`,`Subject_ID`,Examdata.`Student_ID`,`Subject_name`,`exam_days`,Examdata.`exam_dates`,`exam_times` from Examdata join (SELECT Student_ID, count(*), exam_dates FROM Examdata group by Student_ID, exam_dates having count(*) > 1) Examdata1 on Examdata.Student_ID=Examdata1.Student_ID and Examdata1.exam_dates=Examdata.exam_dates order by Examdata1.Student_ID,Examdata1.exam_dates';
$t=mysqli_query($connection,$test);
$out="";
if(isset($_POST["search"]) && isset($_POST["btn"] )) {
$srq=$_POST["search"];
$ser="SELECT * FROM search where
Class_ID like '%$srq%' or Subject_ID like '%$srq%'
or Student_ID like '%$srq%' or Subject_name like '%$srq%' or exam_days like '%$srq%' or
exam_dates like '%$srq%' or exam_times like '%$srq%' ";
$qr=mysqli_query($connection,$ser)or die("Error in query: $qr. " . mysqli_error());
$count=mysqli_num_rows($qr);
if($count == 0){
$out="no";
}
else{
while($ro=mysqli_fetch_array($qr)){
echo '<td> Student ID: ' . $ro["Student_ID"] . '</td></tr>';
echo '<tr style="display:none;">';
echo '<td class="">' . $ro["Class_ID"] . '</td>';
echo '<td class="">' . $ro["Subject_ID"] . '</td>';
echo '<td class="">' . $ro["Subject_name"] . '</td>';
echo '<td class="">' . $ro["exam_days"] . '</td>';
echo '<td class="">' . $ro["exam_dates"] . '</td>';
echo '<td class="">' . $ro["exam_times"] . '</td></tr>';
}
}
}
echo $out;