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 ?

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
include 'config.php';	
    // 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
            $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
// Close connection
   <div class="search-box">
        <input type="text" autocomplete="off" placeholder="Search" />
        <div class="result"></div>

 <script src=""></script>
    $('.search-box input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
            $.get("backend-search.php", {term: inputVal}).done(function(data){
                // Display the returned data in browser
        } else{
    // Set search input value on click of result item
    $(document).on("click", ".result p", function(){

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.


Thanks !!! You save me again :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service