I am doing search engine with ajax php/mysql input file makes suggestions when you type and i want to select some product and it must be inserted in table

I have done ajax request but it is not saving product to table

 <div class="container"> 
    <div>
        <input type="text" id="txt_search" name="txt_search">
    </div>
    <ul id="searchResult"></ul>

<div class="clear"></div>
<div id="userDetail"></div>

</div>

.

<script>
$(document).ready(function(){

$("#txt_search").keyup(function(){
    var search = $(this).val();

    if(search != ""){

        $.ajax({
            url: 'getSearch.php',
            type: 'post',
            data: {search:search, type:1},
            dataType: 'json',
            success:function(response){

                var len = response.length;
                $("#searchResult").empty();
                for( var i = 0; i<len; i++){
                    var id = response[i]['id'];
                    var fname = response[i]['fname'];

                    $("#searchResult").append("<li 
         value='"+id+"'>"+fname+"</li>");

                }

                // binding click event to li
                $("#searchResult li").bind("click",function(){
                    setText(this);
                });

            }
        });
    }

   });

 });

 function setText(element){

  var value = $(element).text();
  var userid = $(element).val();

  $("#txt_search").val(value);
$("#searchResult").empty();

// Request User Details
$.ajax({
    url: 'getSearch.php',
    type: 'post',
    data: {userid:userid, type:2},
    dataType: 'json',
    success: function(response){

        var len = response.length;
        $("#userDetail").empty();
        if(len > 0){
            var username = response[0]['username'];
            var email = response[0]['email'];
            $("#userDetail").append("Username : " + username + "<br/>");
            $("#userDetail").append("Email : " + email);
        }
    }

});
}
</script>

I expect to have this search result selection saved in array and then display this array elements as table

0 Answers