0

Hi guys so i am new to php and trying to create an autocomplete function into my search box. It all works fine and is running but for some reason when i search for the product and it appears i am trying to make it so that the place i search i can just click on it and then it goes to that search box. However for some reason i am not sure why this is.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dashboard</title>
  </head>
  <body>

    <div class="container">
        <input type="text" name='search_term' id="search_term" class="searchFunction">
        <input type="submit" value="Search">
        <div class="dropdown">
            <ul class="result"></ul>
        </div>
    </div>

I was watching a couple of tutorails and the way the guy did it was use this function $('.searchFunction').attr('value', result_value ); however this does not work for me, any help on this matter would be great

P.S i know about the sql injections :) but for now just sorting out one problem at a time

Nevershow2016
  • 570
  • 6
  • 19

1 Answers1

0

Take your click function out of your keyup function and change your click function like I have it below.

$( document ).ready( function() {
    $('.searchFunction').keyup( function( event ) {
       var search_term = $("#search_term").val();

       $.post( document.location.href, { search_term:search_term }, function( data ) {
           $('.result').html( data );    
        });
    });

    $('.result').on('click', 'li', function() {
        var result_value = $(this).text();
        $('#search_term').val(result_value);
        $('.result').html('');
    });
});
DiddleDot
  • 745
  • 5
  • 16