2017-10-13 40 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" 在浏览器中是否可以访问(因为您无法连接到非公共资源)? 您应该避免在网址中使用../../,并且您的链接应该从/或作为完整路径开始