1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Dynamically search SQL table and display on HTML using PHP and JavaScript

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 30, 2024 às 17:52.

  1. Stack

    Stack Membro Participativo

    I have a form where I want the user to select an Organization from a SQL table and when the form is submitted, the ID of the selected organization should be saved to a different table. I researched online and on SO, and this is what I have now. And it does not work. What's wrong? Newbrand.php:

    <form action="newbrand.php" method="post">

    Brand Name: <input type="text" name="bname" /><br><br>
    Ogranization: <input type="text" name="searchbar" id="searchbar"><br><br>
    <script>
    $("#searchbar").keyup(function(){
    var searchTerm = $(this).val();

    $.post('search.php', { search_term: searchTerm}, function(data){

    $(".searchResults").html(data);
    $("#searchUl").css("display", "block");
    });
    });
    </script>
    Organization ID: <input type="hidden" name="gid" value="" /><br><br>
    Gallery ID: <input type="text" name="gid" /><br><br>
    </form>


    Search.php:

    <?php
    include 'db_connect.php';
    $link = mysqli_connect($host, $username, $password, $db);

    $search_term = sanitize(htmlentities($_POST['search_term']));

    if (!empty($search_term)){

    $search = "(SELECT `Organization_Name` FROM `organizations_info` WHERE `Organization_Name` LIKE '%$search_term%' LIMIT 0, 5) ";
    $query = mysqli_query($link, $search);
    $result = mysqli_num_rows($query);

    while ($row = mysqli_fetch_assoc($query)){
    #$user_id = $row['user_id'];
    #$username = $row['username'];
    $orgname = $row['Organization_Name'];
    $check = mysqli_num_rows($query);



    if ($check != 0){
    echo "<a style='text-decoration: none; color: black;' href='newbrand.php?band=$orgname'><li class='searchResults'>" . ucfirst($orgname) . "</li></a>";
    } else {
    echo "<li class='searchResults'>No Results Found</li>";
    }
    }
    }
    ?>

    Continue reading...

Compartilhe esta Página