weixin_33730836 2020-04-15 19:23 采纳率: 0%
浏览 16

使用Ajax在模态上编辑数据

I want to edit my data on a modal and I can't pass my data from JSON to the modal. I tried to print my JSON using console.log() function and it works fine. But when I'm trying to pass the data to my modal, it doesn't work. Here's my script:

    $(document).on('click', '.editBtn', function(e){
    e.preventDefault();
    edit_id = $(this).attr("id");
    $.ajax({
        url:"action.php",
        method:"POST",
        data:{edit_id:edit_id},
        dataType:"json",
        success:function(data){
            // data = JSON.parse(response);
            console.log(data);
            $('#id').val(data.id); //id name of the modal; the hidden type
            $('#fname').val(data.fname);
            $('#lname').val(data.lname);
            $('#email').val(data.email);
            $('#phone').val(data.phone);
        }
    });
});

Here's how I encode my JSON:

    if (isset($_POST['edit_id'])){
    $id = $_POST['edit_id'];

    $row = $db->getUserById($id);
    echo json_encode($row);
}

And here's my code for getUserByID():

public function getUserById($id){
        $sql = "SELECT * FROM users WHERE id = ?";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute([$id]);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

Btw, here's my code for the modal:

<div class="modal fade" id="editModal">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Edit User</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body px-4">
                <form accept="" method="post" id="edit-form-data">
                    <input type="hidden" name="id" id="id">
                    <div class="form-group">
                        <input type="text" name="fname" class="form-control" id="fname" required>
                    </div>
                    <div class="form-group">
                        <input type="text" name="lname" class="form-control" id="lname" required>
                    </div>
                    <div class="form-group">
                        <input type="email" name="email" class="form-control" id="email" required>
                    </div>
                    <div class="form-group">
                        <input type="tel" name="phone" class="form-control" id="phone" required>
                    </div>
                    <div class="form-group">
                        <input type="submit" name="update" id="update" value="Update User" class="btn btn-primary btn-block">
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
  • 写回答

1条回答 默认 最新

  • from.. 2020-04-16 22:26
    关注

    I've already figured it out. My code in script is incomplete. It should be data[0].id etc.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog