duanqiu2064 2013-05-15 19:10
浏览 59
已采纳

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);
}
  • 写回答

1条回答 默认 最新

  • dshun123456 2013-05-15 19:20
    关注

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

    $('.results').html(string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块