Dynamic search bar in jqery with result display problems - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, December 9, 2021

Dynamic search bar in jqery with result display problems

hello how are you? i have a small question i made a dynamic search bar until now it's ok ^^ but the problem i have is that when i type the beginning of a letter or a number i don't have a display limit it takes all the page someone already have a solution? thanks a lot

here is the Jquery

$(document).ready(function () {
    $('#search-stock').keyup(function (){
        $('#result-search').html('');
        var stock = $(this).val();
        if (stock != "") {
            $.ajax({
                type: 'GET',
                url: '../ajax/stock_search.php',
                data: 'stocks=' + encodeURIComponent(stock),
                success: function (data) {
                    if (data != "")
                    {

                        $('#result-search').append(data);
                    } else {
                        document.getElementById('result-search').innerHTML = "" + "<div style='font-size: 20px; text-align: center; margin-top: 10px'> Aucun élement trouver</div>"
                    }
                }
            });
        }
    });

});

here is the php

<?php
session_start();
include('../include/bdd.php');


if(isset($_GET['stocks'])){

    $stock_aema = (string) trim($_GET['stocks']);
    $req = $bdd->prepare("SELECT * FROM stock_aema WHERE model LIKE :model LIMIT 10");
    $req2 = $bdd->prepare("SELECT * FROM stock_aema WHERE id LIKE :id LIMIT 10");
    $req3 = $bdd->prepare("SELECT * FROM stock_aema WHERE aema LIKE :aema LIMIT 10");

    $req->execute(array('model' => "%$stock_aema%"));
    $req = $req->fetchAll();

    foreach ($req as $r){
        ?>
        <div style="margin-top: 20px ; border-bottom: 2px solid #ccc">
            <?= $r['id'] . " " . $r['model']. " " . $r['aema']?>
        </div>
        <?php
    }
    $req2->execute(array('id' => "%$stock_aema%"));
    $req2 = $req2->fetchAll();

    foreach ($req2 as $s){
        ?>
        <div style="margin-top: 20px ; border-bottom: 2px solid #ccc">
            <?= $s['id'] . " " . $s['model']. " " . $s['aema']?>
        </div>
        <?php
    }
    $req3->execute(array('aema' => strval("%$stock_aema%")));
    $req3 = $req3->fetchAll();

    foreach ($req3 as $x){
        ?>
        <div style="margin-top: 20px ; border-bottom: 2px solid #ccc">
            <?= $x['id'] . " " . $x['model']. " " . $x['aema']?>
        </div>
        <?php
    }
}
?>


source https://stackoverflow.com/questions/70280431/dynamic-search-bar-in-jqery-with-result-display-problems

No comments:

Post a Comment