Search AJAX

Hello, i find in web this search code. Its nice but if have string with space see onli first word.
if string is “Radoslav Todorov Stefanov” on pressed “r” have result “Radoslav Todorov Stefanov” , on pressed “t” don’t have result.

It’s easy to be modify and can search all string ?

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
include 'config.php';	
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM playlist WHERE name LIKE ?";
    
    if($stmt = $conn->prepare($sql)){
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("s", $param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if($stmt->execute()){
            $result = $stmt->get_result();
            
            // Check number of rows in the result set
            if($result->num_rows > 0){
                // Fetch result rows as an associative array
                while($row = $result->fetch_array(MYSQLI_ASSOC)){
                    echo "<p>" . $row["name"] . "</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }
     
    // Close statement
    $stmt->close();
}
 
// Close connection
$conn->close();
?>
   <div class="search-box">
        <input type="text" autocomplete="off" placeholder="Search" />
        <div class="result"></div>
    </div>


 <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $('.search-box input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(inputVal.length){
            $.get("backend-search.php", {term: inputVal}).done(function(data){
                // Display the returned data in browser
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });
    
    // Set search input value on click of result item
    $(document).on("click", ".result p", function(){
        $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
    });
});
</script>

LIKE is tricky sometimes to use in a query. First, let’s explain how your version is set up.
You use the TERM with a % added to the end. This means it uses your EXACT TERM and anything that follows. TERM% means look for anything that STARTS with TERM and don’t care about what follows.

You might want to change this to %TERM% instead. This would locate anything that CONTAINS your TERM instead of just starting with it.

Hope that helps explain LIKE enough to get it working for you.

2 Likes

Thanks !!! You save me again :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service