Emilo
Emilo

Reputation: 35

How to check if json data return is empty with jquery

i have a live searcher thats when its data its shows i need to alert if the data is empty

this is the jquery:

$(document).ready(function() {
        $('#q').on('input', function() {
            var searchKeyword = $(this).val();
            if (searchKeyword.length >= 3) {
                $.post('/files/assets/php/ajax/search.php', { q: searchKeyword }, function(data) {
                    $('ul#content').show();
                    $('ul#content').empty()
                    $.each(data, function() {
                         if ( data.length == 0 ) {
                        $('ul#content').append('<li style="text-align: center;font-weight:bold;"><font color="white">empty</font></a></li>');
                        }else{
$('ul#content').append('<li style="text-align: center;font-weight:bold;"><a href="/store/' + this.id + '/' + this.seo + '"><font color="white">' + this.title + '</font></a></li>');
                        }
                    });
                }, "json");
            }
        });
    });

and the php:

$conexion = mysqli_connect($serv,$user,$pass,$base);

$arr = array();
if (!empty($_POST['q'])) {
    $keywords = $Main->limpiar($_POST['q']);
    $mysqli = $conexion;
    $result = $mysqli->query("SELECT cat, titulo FROM pinturas WHERE titulo LIKE '%".$keywords."%' OR cat LIKE '%".$keywords."%'");
    if ($result->num_rows > 0) {
        while ($obj = $result->fetch_array()) {
            $seo =  str_replace(" ","_",$obj['titulo']);
            $arr[] = array('id' => $obj['cat'], 'title' => $obj['titulo'], 'seo' => $seo);
        }
    }else{
        $arr[] = array('id' => '', 'title' => '', 'seo' => '');
        }
}
echo json_encode($arr);

i want to if the data is empty shows the append empty

but it dont work

Upvotes: 2

Views: 917

Answers (2)

Gus Ortiz
Gus Ortiz

Reputation: 662

Assuming the data is a JSON parsed object you can:

Object.keys(data).length === 0

Or

JSON.stingify(data) === '{}'

Upvotes: 1

Frebin Francis
Frebin Francis

Reputation: 1915

Check length of the records before iteration (using $.each in jquery API)

    <script>
    $(document).ready(function () {
        $('#q').on('input', function () {
            var searchKeyword = $(this).val();
            if (searchKeyword.length >= 3) {
                $.post('/files/assets/php/ajax/search.php', { q: searchKeyword }, function (data) {
                    console.log(data);
                    $('ul#content').show();
                    $('ul#content').empty()
                    if (data.length == 0) {
                        $('ul#content').empty().append('<li style="text-align: center;font-weight:bold;"><font color="white">empty</font></a></li>');
                    }
                    else {
                        $.each(data, function () {
                            $('ul#content').append('<li style="text-align: center;font-weight:bold;"><a href="/store/' + this.id + '/' + this.seo + '"><font color="white">' + this.title + '</font></a></li>');
                        });
                    }
                });
            }
        });
    });
</script>

Worked for me.

Hope this helps.

Upvotes: 0

Related Questions