doupin8555 2013-10-25 11:59
浏览 44
已采纳

ajax脚本加载新的HTML而不是附加到当前页面...?

Once again, I'm stumped. I have a little search field and button to search a mysql db. I want it to append the results in the current page instead of reloading or loading a separate results page.

Here's the script:

<script>
$(function() {

$(".search_button").click(function() {
    // getting the value that user typed
    var searchString    = $("#search_box").val();
    // forming the queryString
    var data            = 'search='+ searchString;
    //var data = searchString;

    // if searchString is not empty
    if(searchString) {
        // ajax call
        $.ajax({
            console.log(data);
            type: "POST",
            url: "do_search.php",
            data: data,
            beforeSend: function(html) { // this happens before actual call
                $("#results").html(''); 
                $("#searchresults").show();
                $(".word").html(searchString);
           },
           success: function(html){ // this happens after we get results
                $("#results").show();
                $("#results").append(html);
          }
        });    
    }
    return false;
});

});
</script>

Here's the originating page that is supposed to be appended to, within the ul tags:

<div id="SEARCH2" class="panel">
<div style="margin:20px auto; text-align: center;">
<form method="post" action="do_search.php">
    <input type="text" name="search" id="search_box" class='search_box'/>
    <input type="submit" value="Search" class="search_button" /><br />
</form>
</div>      
<div>

<div id="searchresults">Search results :</div>
<ul id="results" class="update">
</ul>

</div>
</div>

And here's the do_search.php that's supposed to insert some html within the ul tags:

<?php

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

include('db.php');

$word = mysql_real_escape_string($_POST['search']);
$word = htmlentities($word);

$sql = "SELECT id, last_name, first_name, u_name, skype_id, primary_location, phone, status FROM users WHERE (last_name LIKE '%" . $word . "%' OR
first_name LIKE '%" . $word . "%' OR u_name LIKE '%" . $word . "%' OR skype_id LIKE '%" . $word . "%' OR phone LIKE '%" . $word . "%') ORDER BY last_name";

$fetched = mysql_query($sql)or die('query error'.mysql_error());
$end_result = '';
if($fetched){
    while($row = MySQL_fetch_array($fetched)):
          //TEST TO SEE IF THIS WORKS
        $result         = $row['id'];
        // we will use this to bold the search word in result
        $bold           = '<span class="found">' . $word . '</span>';    
        $end_result     .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';            
    endwhile;
    echo $end_result;
}
else{
    echo '<li>No results found</li>';
}    
}
//echo 'crap2';
?>
  • 写回答

1条回答 默认 最新

  • doukang2003 2013-10-25 12:19
    关注

    Remove the console.log(data); from your AJAX call. And if you're constantly appending, remove the $("#results").html(''); from beforeSend:

    $.ajax({
        type: "POST",
        url: "do_search.php",
        data: data,
        beforeSend: function(html) { // this happens before actual call
            $("#results").html(''); 
            $("#searchresults").show();
            $(".word").html(searchString);
       },
       success: function(html){ // this happens after we get results
            $("#results").show();
            $("#results").append(html);
      }
    });  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入