duanqiang6501 2019-01-23 08:13
浏览 76


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)
    $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>

        <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 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 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 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 id="preview" style="display:none";>

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

            <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>-->

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
        url : "<?php echo site_url('user/ajax_edit/')?>/" + id,
        type: "GET",
        dataType: "JSON",
        success: function(data)

            $(".#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").append("<img src='../assets/img_user/"+data.user_image+"' class='contentimg'>");
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?