duanqiang6501 2019-01-23 08:13
浏览 76
已采纳

如何在视图中使用codeigniter和ajax通过id显示图像和数据

please help me. I'm making an application using codeigniter and ajax, when uploading a photo successfully. But when I click edit button, the photo doesn't appear in the bootstrap modal to be updated.

Here some code, I use the datatable server side. User_model.php

public function get_by_id($id)
{
    $this->db->from($this->table);
    $this->db->where('user_id',$id);
    $query = $this->db->get();

    return $query->row();
}

User.php (controller)

public function ajax_edit($id)
{
    $data = $this->User_model->get_by_id($id);
    echo json_encode($data);
}

user.php (view using modal bootstrap)

<div class="modal fade" id="modal_form" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 class="modal-title">Tambah Pengguna</h3>
        </div>

        <form action="#" id="form" class="form-horizontal" >
            <input type="hidden" value="" name="id"/>
            <div class="modal-body">

                <div class="form-group">
                    <label class="control-label col-xs-3" >Username <font color="red>">*</font></label>
                    <div class="col-xs-9">
                        <input name="userNm" class="form-control" type="text" placeholder="" style="width:335px;"required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-xs-3" >Nama Pengguna <font color="red>">*</font></label>
                    <div class="col-xs-9">
                        <input name="nama" class="form-control" type="text" placeholder="" style="width:335px;" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-xs-3" >Email <font color="red>">*</font></label>
                    <div class="col-xs-9">
                        <input name="email" class="form-control" type="text" placeholder="" style="width:335px;" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-xs-3" >Upload Foto <font color="red>">*</font></label>
                    <div class="col-xs-9">
                        <input type="file" name="fupload" required/>
                    </div>
                </div>

                <div id="preview" style="display:none";>
                </div>


                <div><br></div>
                <font color="red">*</font> : kolom wajib di isi.
            </div>

            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Batal</button>
                <button class="btn btn-info" id="btn_simpan" type="submit" onclick="save()">Simpan</button>
                <!--<button class="btn btn-info" id="btn_simpan" type="submit" >Simpan</button>-->
            </div>
        </form>
    </div>
</div>

and here the ajax for display data into edit form.

function edit_user(id)
{
    save_method = 'update';
    $('#form')[0].reset(); // reset form on modals
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string

    //Ajax Load data from ajax
    $.ajax({
        url : "<?php echo site_url('user/ajax_edit/')?>/" + id,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {
            $('[name="id"]').val(data.user_id);
            $('[name="userNm"]').val(data.username);
            $('[name="nama"]').val(data.complete_name);
            $('[name="email"]').val(data.user_email);
            //$('[name="fupload"]').val(data.user_image);

            $(".#preview").attr("src", './assets/img_user/'+val(data.user_image);

            $('#modal_form').modal('show'); // show bootstrap modal when complete loaded
            $('.modal-title').text('Edit Data Pengguna'); // Set title to Bootstrap modal title

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error get data from ajax');
        }
    });
}

I hope anyone can help me. Thanks before.

  • 写回答

2条回答 默认 最新

  • dounaoji2054 2019-01-23 08:25
    关注

    I also show one thing in our code that div has not image tag. So you have to add this.

     $("#preview").html('');
    
     $("#preview").append("<img src='../assets/img_user/"+data.user_image+"' class='contentimg'>");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。