skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Assuming the data is a JSON parsed object you can:

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

    Or

    JSON.stingify(data) === '{}'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search