2017-08-19 7 views
0

スクリプトコードに問題があります。私は検索の結果を表示する警告ボックスを持っていますが、問題はそれが現在のものではなく前の結果を表示していることです。初めて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> 
+0

あなたは何を期待していますか?私はあなたのページがちょうどリロードし、それが前の検索の結果を表示する理由だと思います。 –

+0

'$(document).ready'の警告を' form.submit'で表示しないようにしてください。 –

+0

@u_mulderはいです。それを修正する方法を教えてください。 – Peslis

答えて

0

PHPパーツが実行されるとどうなるかを見てみましょう。あなたのページが初めて表示されたら、$ qは空です。だからあなたのSQLは結果を返さないでしょう。 $出力は'<h2 style="color:white;">No player found!</h2>';に設定され、$ s []は設定されません。これは、フォームをsumbitときだ

<script> 
      $(document).ready(function(){ 
       $("form").submit(function(){ 
        var player = null; 
        alert("players : " + player); 
    }); 
}); 
</script> 

はなぜ、アラートがヌル示しています... JavaScriptタグ内のページに書かれているものです。 2回目にページを表示すると、$ qには値があり、$ sにはSQL呼び出しが挿入されます。スクリプトは次のようになります。

<script> 
      $(document).ready(function(){ 
       $("form").submit(function(){ 
        var player = "Some Playername"; 
        alert("players : " + player); 
    }); 
}); 
</script> 

アラート機能は、フォームを送信するときにのみ実行され、アラートボックス内に名前が表示されます。しかしもちろん、それまでに次のプレーヤー名が表示されることを期待しています。

+0

私は今理解しています。私はそれを私が推測する方法を見つけ出すでしょう。インフォメーション – Peslis

+0

のためにありがとう私はページの再読み込みでfuctionを提出すると置き換えられますか? – Peslis

関連する問題