php - Codeigniter 3应用程序: update MySQL table column and the corresponding view through Ajax

标签 php jquery mysql ajax codeigniter

我正在使用 CodeIgniter 3 和 Twitter Bootstrap 开发注册和登录应用程序。

我有一个“users”MySQL 表和一个相应的“users.php” View ,该 View 以 HTML 格式呈现“users”表,如下图所示:

enter image description here

Bootstrap 表中的“操作”列的每一行都有一个“启用”或“禁用”按钮,具体取决于用户的状态。这部分 View 的代码是:

// Status column
<td>
   <?php if ($user->active == 1) {
    echo '<span class="text-success">' . 'Enabled' . '</span>';
    } else {
    echo '<span class="text-danger">' . 'Disabled' . '</span>';
    }
   ?>
</td>

// Enable/Disable buttons
<?php if ($user->active == 1) { ?>
    <a href="<?php echo base_url(); ?>users/deactivate/<?php echo $user->id ?>" title="Deactivate" class="btn btn-success btn-xs state-change" data-role="deactivate" data-id="<?php echo $user->id ?>"><span class="glyphicon glyphicon-ban-circle"></span> Disable</a>
<?php } else { ?>
   <a href="<?php echo base_url(); ?>users/activate/<?php echo $user->id ?>" title="Activate" class="btn btn-success btn-xs state-change" data-role="activate" data-id="<?php echo $user->id ?>"><span class="glyphicon glyphicon-ok"></span> Enable</a>
<?php } ?>

我通过 AJAX 激活/停用用户,无需刷新页面:

$('.state-change').on('click', function(evt) {
    evt.preventDefault();
    var id = $(this).data('id');
    var role = $(this).data('role');
    if (role == "activate") {
        var stateUrl = 'users/activate/';
    } else {
        var stateUrl = 'users/deactivate/';
    }
    $.ajax({
        url: stateUrl + id,
        method: 'GET',
        dataType: 'php',
        success: function(){
            console.log(id);
            console.log(role);
        }
    });
});

问题在于有关用户状态的数据不会返回到 View 中,并且“状态”和“操作”列无法正确呈现。

我希望我不必使用 jQuery 的 html() 方法或类似方法从成功回调中“静态”更新 View 。

function(){
    console.log(id);
    console.log(role);
    //change columns html here
}

我该怎么做才能“动态”更新 View ?

最佳答案

以上答案已经足够了 - 根据您的情况,您必须使用 JavaScript。看来麻烦在于从动态网页到动态 HTML 的跨越。

请阅读此处摘要的最后一行: https://en.wikipedia.org/wiki/Dynamic_HTML

关于php - Codeigniter 3应用程序: update MySQL table column and the corresponding view through Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290823/

相关文章:

php - preg_match() : No ending delimiter '/'

javascript - 这会在 IE 中泄漏吗?

javascript - 全局 iframe 文档

php - Doctrine - 来自的子查询

PHP MySQLi 准备好的语句不工作

php - 列计数与第 1 行的值计数不匹配 - PHP、MySql

java - 公钥处理 Java/PHP

php - 读取标题后中止上传大文件

javascript - 在 WordPress : throw all jQuery plugins in, 中使用 JavaScript 或者是否有条件加载的方法?

mysql - 从 SUM 中取最大值