drg14799 2017-04-06 13:10
浏览 83

发出AJAX请求并在同一页面上返回数据

Here's snippet from my code: page.php

<input type="text" id="search_query" style="float: right" placeholder="Search" />
<div id="result"></div>
<table>
    <tr>
        <th><input type="checkbox" name="select_all" id="select_all" value=""/></th>
        <th>Title</th>
        <th>Author</th>
        <th>Date Published</th>
        <th>Actions</th>
    </tr>
    <?php
    if (isset($_POST['query'])) {
        $query = $_POST['query'];
        $res = search_articles($query); // a function I have to query the database with SQL 'LIKE'
    } else {
        $res = view_articles(); // another function
    }
    while ($row = $res->fetch_assoc()) {
        echo '<tr>';
        echo '<td><input type="checkbox" name="checked_id[]" class="checkbox" value="' . $row['id'] . ' ?>"/></td>';
        echo '<td>' . $row['title'] . '</td>';
        echo '<td>' . $row['author'] . '</td>';
        echo '<td>' . $row['date_published'] . '</td>';
        echo '<td>
                <a href="?del=' . $row['id'] . '">Delete</a> | 
                <a href="?edit=' . $row['id'] . '">Edit</a>
            </td>';
        echo '</tr>';
    }
    ?>
</table>

and the ajax request: (also in page.php)

function load_data(query) {
    $.ajax({
        url: "",
        method: "POST",
        data: {query: query},
        success: function (data) {
            $('#result').html(data);
        }
    });
}
$('#search_query').keyup(function () {
    var search = $(this).val();
    if (search != '') {
        load_data(search);
    } else {
        load_data();
    }
});

Basically, I have a search bar where the user can type anything and then the table where I'm showing the columns (taken from a database table) will change dynamically.

The code I have above is working fine but every time I type anything on the search bar, an exact copy of the page is being displayed (everything in my page gets displayed twice). Is there a way I can make the AJAX request return the table only and not the entire page? (without moving the entire code in a separate php file as I want to accomplish this in a single file)

I believe the problem is because I'm passing the data (which contains the whole page) to the #result div. I've tried looking for a solution and been tweaking my code for the last 3 hours or so but to no avail. I'm relatively new to these web technologies (God there are so many!) and I hope someone can help.

  • 写回答

1条回答 默认 最新

  • dptgpyl61857413 2017-10-23 15:31
    关注

    Check the solution posted here Ajax to PHP on the same page

    Basically it suggest that you shouldn't echo rather than exit() your response. Otherwise, the remainder of the page is "loaded" again after the if(isset... is finished.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?