0

I mixed an "ajax select" and "while scrolling load data" script, and this is working, but I don't know how to print "not found data" in div.status when the output variable (on animals.php) is empty.

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Animals</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>

<div class="search">

<div class="filter category">
    <select name="category" id="category">
        <option value="">All</option>
        <option value="free">Free</option>
        <option value="lost">Lost</option>
        <option value="found">Found</option>
    </select>
</div>

<div class="filter chipnumber">
    <input type="text" name="chipnumber" id="chipnumber"></div>
</div>

<div class="send">

<button type="submit" id="submit">Search</button>

</div>

</div>

<script>

$(document).ready(function() {

    var animal_limit = 6;
    var animal_start = 0;
    var animal_action = 'inactive';

    function load_animal_data() {

        var category = $('#category').val();
        var chipnumber = $('#chipnumber').val();

        $.ajax({

            url: "animals.php",
            method: "POST",
            data: {animal_limit:animal_limit, animal_start:animal_start, animal_action:animal_action, category:category, chipnumber:chipnumber},
            success:function(data) {

                $('div.animals').append(data);

                if (data == '') {

                    animal_action = 'active';

                } else {

                    animal_action = 'inactive';

                }

            }

        });

    }

    load_animal_data();

    function search() {

        var category = $('#category').val();
        var chipnumber = $('#chipnumber').val();

        animal_start = 0;

        load_animal_data();

    }

    $('#search').on('click', function() {

        search();

    });

    $(window).scroll(function () {

        if ($(window).scrollTop() + $(window).height() > $('div.animals').height() && animal_action == 'inactive') {

            animal_action = 'active';

            animal_start = animal_start + animal_limit;

            setTimeout(function() {

                load_animal_data();

            }, 1000);

        }

    });

});

</script>

<div class="animals"></div>
<div class="status"></div>

</body>
</html>

animals.php

<?php

$connect = mysqli_connect("localhost", "root", "", "petsdata");

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_set_charset($connect,"utf8");

$output = '';

$animal_start = $connect->real_escape_string($_POST["animal_start"]);
$animal_limit = $connect->real_escape_string($_POST["animal_limit"]);

$category = $connect->real_escape_string($_POST["category"]);
$chipnumber = $connect->real_escape_string($_POST["chipnumber"]);

if (isset($animal_start, $animal_limit, $category, $chipnumber)) {

    if (!empty($category) && !empty($chipnumber)) {

        $query = mysqli_query($connect, "SELECT * FROM animals WHERE chipnumber LIKE '%".$chipnumber."%' AND category = '".$category."' ORDER BY id LIMIT ".$animal_start.", ".$animal_limit."");

    }

    else if (!empty($category)) {

        $query = mysqli_query($connect, "SELECT * FROM animals WHERE category = '".$category."' ORDER BY id LIMIT ".$animal_start.", ".$animal_limit."");

    }

    else if (!empty($chipnumber)) {

        $query = mysqli_query($connect, "SELECT * FROM animals WHERE chipnumber LIKE '%".$chipnumber."%' AND status = '1' ORDER BY id DESC LIMIT ".$animal_start.", ".$animal_limit."");

    }

    else {

        $query = mysqli_query($connect, "SELECT * FROM animals ORDER BY id DESC LIMIT ".$animal_start.", ".$animal_limit."");

    }

        while ($row = mysqli_fetch_array($query)) {

            $output .= '<div class="animal">';
                $output .= '<span>Category: ' . $row["category"] . '</span>';
                $output .= '<span>Chipnumber: ' . $row["chipnumber"] . '</span>';
            $output .= '</div>';

        }

}

echo $output;

?>
patesz
  • 65
  • 1
  • 7
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Oct 30 '17 at 21:45

1 Answers1

1
if($query->num_rows > 0){
   //Proceed as normally 
}else{
   $output = 'No data Found';
}
Kisaragi
  • 2,198
  • 3
  • 16
  • 28
  • I tried this, but every scrolling after print the 'No data Found' repeatedly. For example: "No data Found No data Found No data Found" – patesz Oct 30 '17 at 22:06
  • You're better off returning a variable to your AJAX function that checks whether result is 0. If it is, then stop processing.. – giolliano sulit Oct 31 '17 at 00:27