我遇到了腳本代碼問題。我有一個警告框顯示搜索結果,但問題是它顯示的是前一個結果,而不是當前結果。第一次給出null時,它通過給出以前的結果來工作。哪裏不對? 。提前感謝警報框故障
<?php
include_once('dbconnect.php');
if(isset($_POST['search'])){
$q = $_POST['q'];
$query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'");
//Replace table_name with your table name and `thing_to_search` with the column you want to search
$count = mysqli_num_rows($query);
if($count == "0" || $q == ""){
$output = '<h2 style="color:white;">No player found!</h2>';
}else{
while($row = mysqli_fetch_array($query)){
$s[] = $row['userIngame']; // Replace column_to_display with the column you want the results from
$output = '<h2 style="color:white;">There are '.$count.' players </h2><br>';
}
}
}
echo json_encode($s);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("form").submit(function(){
var player = <?php echo json_encode($s); ?>;
alert("players : " + player);
});
});
</script>
你期望什麼?我想你的頁面只是重新加載,這就是爲什麼顯示以前的搜索結果。 –
嘗試在不在'form.submit'上顯示'$(document).ready'的提醒。 –
@u_mulder是的,它的確如此。請告訴我如何修復它 – Peslis