duanqiu2064
2013-05-15 19:10
浏览 54

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

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);
}

图片转代码服务由CSDN问答提供 功能建议

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

HTML

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

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){
 \  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; 
}); 
   
 
 

PHP / SQL

  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); 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dshun123456 2013-05-15 19:20
    已采纳

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

    $('.results').html(string);
    
    点赞 评论

相关推荐 更多相似问题