-1

I have a selection menu with month names (values are numbers from 1 to 12):

Inside I want to fetch data from database. So if chosen October then shows tournaments of October and so on...

SQL code is right (tested).

It looks like ajax is not even finding fetch_tournaments.php file... or maybe just somewhere I made a mistake...

My 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';
}

?>

Solved... there was a typo

Nikita Ribakovs
  • 305
  • 3
  • 11
  • 1
    [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Oct 13 '17 at 19:59
  • 1
    [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a class for](https://github.com/GrumpyCrouton/GrumpyPDO) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Oct 13 '17 at 20:03

2 Answers2

0

First of all, as I don't know your folder structure, did you try having both files in the same folder, maybe you are having an issue to include the php file.

I think that is the first thing we should try.

0

Are you sure that url: "../../includes/funtions/ajax/fetch_tournaments.php" Is accessible in browser (because you can't make links to non public resources)? You should avoid using ../../ in urls and your links should start from / or as full path

Rafael
  • 326
  • 2
  • 6