如何在表格中动态添加搜索结果?

时间:2019-05-10 09:09:26

标签: php ajax

我正在使用ajax php / mysql输入文件做搜索引擎,当您键入内容时,我会提出建议,我想选择一些产品,并且必须将其插入表中

我已经完成了ajax请求,但没有将产品保存到表中

 <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>

我希望将此搜索结果选择保存在数组中,然后将该数组元素显示为表

0 个答案:

没有答案