2017-10-13 74 views
-1

我有一個月份名稱的選擇菜單(值是數字從1到12):數據沒有抓取PHP MYSQL AJAX

裏面我想從數據庫中獲取數據。所以如果選擇10月然後顯示10月的錦標賽等等......

SQL代碼是正確的(測試)。

它看起來像阿賈克斯甚至沒有找到fetch_tournaments.php文件,或者......只是地方我犯了一個錯誤......

我的AJAX:

$(document).ready(function(){ 

    $('#monthSelector').change(function(){ 

     var month = $(this).val(); 

     alert(month); 

     $.ajax({ 

      url:"../../includes/functions/ajax/fetch_tournaments.php", 
      method:"POST", 
      data:{month: month}, 
      dataType:"text", 
      success:function(data) 
      { 
       $('#tournamentList').html(data); 
      } 

     }); 

    }); 

}); 

fetch_tournaments.php:

<?php 

require_once '../../../config/init.php'; 

$sql = "SELECT * FROM tournaments WHERE itfnt = 'ITF' AND MONTH(date) = '".$_POST["month"]."' ORDER BY date ASC"; 
$result = $mysqli->query($sql); 
$num_rows = $result->num_rows; 
if($num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 
     echo '<div class="col-md-6"> 
       <div class="card"> 
        <div class="card-block"> 
         <h6 class="category text-danger"> 
          <i class="now-ui-icons location_pin"></i> Latvia, Riga 
         </h6> 
         <h5 class="card-title"> 
          <a href="#pablo">RIGA AUTUMN CUP 1</a> 
         </h5> 
         <div class="card-footer"> 
          <div class="author"> 
           <img src="assets/img/lpts.png" alt="..." class="avatar img-raised"> 
           <span>LPTS</span> 
          </div> 
          <div class="stats stats-right"> 
           <i class="now-ui-icons ui-1_calendar-60"></i> 25.10.17 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
      '; 
    } 
} else { 
    echo 'No tournaments that month'; 
} 

?> 

解決......有一個錯字

+1

[您是否觀看過瀏覽器開發人員工具中的AJAX請求/響應?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?你在網絡服務器上運行它嗎?](http://jayblanchard.net/basics_of_jquery_ajax.html) –

+1

[Little Bobby](http://bobby-tables.com/)說** [你可能處於危險之中對於SQL注入攻擊](https://stackoverflow.com/q/60174/)**。通過[參數化查詢](https://stackoverflow.com/a/4712113/5827005)瞭解[準備好的陳述](https://en.wikipedia.org/wiki/Prepared_statement)。我推薦'PDO',我[寫了一個類的](https://github.com/GrumpyCrouton/GrumpyPDO),使它非常容易,乾淨,並且比使用非參數化查詢更安全。此外,[本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)可以幫助您在'MySQLi'和'PDO'之間進行選擇 – GrumpyCrouton

回答

0

首先,因爲我不知道你的文件夾結構,你有沒有嘗試在同一個文件夾中有兩個文件,也許你有一個問題,包括PHP文件。

我認爲這是我們應該嘗試的第一件事。

0

您確定該網址: "../../includes/funtions/ajax/fetch_tournaments.php" 在瀏覽器中是否可以訪問(因爲您無法連接到非公共資源)? 您應該避免在網址中使用../../,並且您的鏈接應該從/或作爲完整路徑開始