dongmu3187 2016-08-14 20:25 采纳率: 0%
浏览 170

如何刷新页面数据以显示使用ajax调用更改的动态表数据而不重新加载页面?

I've got an ajax call that changes, at the moment just the first name for testing purposes, of a contact using a bootstrap modal. On success I want the page data refreshed, but not to actually reload the page. So once the user clicks save changes the modal disappears and the table data with refresh with newly changed name. Here's the code I've got so far:

<?php

$db = new mysqli('127.0.0.1', 'root', '', 'test');

require_once 'Model.php';
require_once 'Contact.php';

$contacts = Contact::find_all();

?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <title>Test</title>

    <script>
        $(document).ready( function() {
            $("button").click(function() {
                var clicked = this.id;

                $.ajax({
                    url:      'edit.php',
                    dataType: 'json',
                    data: {
                        action: "display",
                        id: this.id
                    },
                    success:  function(data) {
                        $("#fname").val(data.first_name);

                        $("#save").click(function() {
                            $.ajax({
                                url:      'edit.php',
                                dataType: 'json',
                                data: {
                                    action: "save",
                                    id: clicked,
                                    first_name: $("#fname").val()
                                },
                                success:  function(data) {
                                    console.log(data);
                                    //refresh page here
                                }
                            });
                        });
                    }
                });
            });
        });
    </script>
</head>
<body>
<table class="table table-striped" id="table">
    <thead>
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Email</th>
            <th>Edit</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach($contacts as $contact): ?>
        <tr>
            <td><?php echo $contact->first_name ?></td>
            <td><?php echo $contact->last_name ?></td>
            <td><?php echo $contact->email ?></td>
            <td>
                <button type="button" class="btn btn-primary btn-lg" name="" id="<?php echo $contact->id ?>" data-toggle="modal" data-target="#myModal">
                    Edit Contact
                </button>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                <input type="text" name="fname" id="fname">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" id="save" class="btn btn-primary save" data-dismiss="modal">Save changes</button>
            </div>
        </div>
    </div>
</div>

<script>
</script>

</body>
</html>

As you can probably tell I am very new to AJAX and JS/jQuery.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-08-14 20:49
    关注

    You need to create the table "on the fly" one data is back from AJAX call - which happens on the callback function specified in success option on $.ajax() call. Right now you simply show the data coming back on a console. You need to clear what is in your (by calling e.g. $('tbody').html('') ) and then basing on the data that is in "data" parameter create the approriate structure of a table inside... Helpful methods you might want to look for in JQuery docs might be:

    .each()
    .append()
    .html()
    

    Hope the above helps :)

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行