jQuery Ajax PHP搜索栏只能运行一次

我为我的网站博客文章构建了一个小型搜索引擎,我用Ajax提交表单然后 返回结果并用新的搜索结果替换旧内容的问题是,如果我搜索一次它的工作,但如果我再次搜索它只显示第一次搜索的结果。 任何帮助都会提前感谢!</ p>

HTML </ strong> </ p>

 &lt; form action =“handlers /  handler-search.php“class =”align-right“method =”post“&gt; 
&lt; input type =”text“name =”term“placeholder =”Search app reviews ...“&gt;
&lt; ; input type =“submit”value =“Go”&gt;
&lt; / form&gt;
</ code> </ pre>

jQuery Ajax </ strong> </ p>

  $('form')。submit(function(event){

event.preventDefault();

$(“。content”)。fadeOut(750) ;

$ .ajax({
type:“POST”,
url:$('form')。attr('action'),
dataType:“json”,
data:$( “form”)。serialize()
})。done(function(data){

var string =“”;

$ .each(data,function(i,item){
\ n string + =“&lt; section class ='widget six'&gt; \
&lt; a href ='index.php?art_id =”+ item.art_id +“'&gt; \
&lt; div class ='content '&gt; \
&lt; h1&gt;“+ item.art_title +”&lt; / h1&gt; \
&lt; / div&gt; \
&lt; / a&gt; \
&lt; / section&gt;“;

});

$('。results')。replaceWith(string);
$(”。content“)。fadeIn(750);
}) ;

返回false;
});
</ code> </ pre>

PHP / SQL </ strong> </ p>

  if(isset($ _ POST ['term'])){

$ statement_search = $ db-&gt; prepare(“SELECT * FROM app_articles WHERE art_title LIKE:term”);

$ statement_search-&gt; setFetchMode(PDO :: FETCH_ASSOC);

$ statement_search-&gt; execute(array(':term'=&gt; '%' $ _ POST [ '术语'] '%'))。。

$ data = $ statement_search-&gt; fetchAll();

echo json_encode($ data);
}
</ code> </ pre>
</ div>

展开原文

原文

I've build a small search engine for my websites blog articles, I'm submitting the form with Ajax and then returning the results and replacing the old content with the new search results the problem is that if I search once it works but if I search again it just displays the results from the first search. Any help would be great thanks in advance!

HTML

<form action="handlers/handler-search.php" class="align-right" method="post">
    <input type="text" name="term"  placeholder="Search app reviews...">
    <input type="submit" value="Go">
</form>

jQuery Ajax

    $('form').submit(function (event) {

    event.preventDefault();

    $(".content").fadeOut(750);

    $.ajax({
        type: "POST",
        url : $('form').attr('action'),
        dataType: "json",
        data: $("form").serialize()
    }).done(function(data) {

        var string = "";

        $.each(data, function(i,item) {

         string += "<section class='widget six'> \
         <a href='index.php?art_id="+item.art_id+"'> \
         <div class='content'> \
         <h1>"+item.art_title+"</h1> \
         </div> \
         </a> \
         </section>";

        });

        $('.results').replaceWith(string);
        $(".content").fadeIn(750);
    });

   return false;
});

PHP / SQL

if(isset($_POST['term'])) {

$statement_search = $db->prepare("SELECT * FROM app_articles WHERE art_title LIKE :term");

$statement_search->setFetchMode(PDO::FETCH_ASSOC);

$statement_search->execute(array(':term' => '%'.$_POST['term'].'%'));   

$data = $statement_search->fetchAll();

echo json_encode($data);
}

1个回答



您要删除结果元素,而应替换它的内容。</ p>

   $('。results')。html(string); 
</ code> </ pre>
</ div>

展开原文

原文

You are removing the results element, you should instead replace it's contents.

$('.results').html(string);

dro7152
dro7152 有道理,非常感谢!
7 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐