我想在Codeigniter中调用Ajax时在img标签中显示图像

时间:2018-11-03 04:36:07

标签: php ajax codeigniter

我想在img标签中显示图像。在CodeIgniter中调用ajax时。 这是从数据库接收数据并将其显示在引导模型中的代码。但主要问题是我想在img标签中显示图片,但不显示。

        $(".Edit-modal").on("shown.bs.modal", function (e) {
        var button = $(e.relatedTarget); 
        var ID = button.parents("tr").attr("data-id");
        var modal = $(this);
        $.ajax({
        url: "'.base_url().'Employees/master_get_employees",
        data: {ID:ID},
        type: "POST",
        success:function(output){
        try{
        var outputData = JSON.parse(output);
modal.find("#EditImage").attr("'.base_url().'src",outputData.pic);
        }
        catch(ex){
        var split = output.split("::");
        if(split[0] === "FAIL"){
        Shafiq.notification(split[1],split[2])
        }else{
        Shafiq.notification("Could Not Load Data, Please Contact System Administrator For Further Assistance","error");
        }
        }
        }
        });
        });

这是Img标签,我要在其中显示图像。

<div class="col-md-3">
<div class="form-group">
<label for="EditcontactNoSelector">Employee Picture</label>
<img src="" id="EditImage" alt="Not Found">
</div>
</div>

这是从数据库中获取数据的功能

public function master_get_employees()
    {
        if ($this->input->post()) { //If Any Values Posted
            if ($this->input->is_ajax_request()) { //If Request Generated From Ajax
                $ID = $this->input->post('ID');
                if (!isset($ID) || !is_numeric($ID)) {
                    echo "FAIL::Something went wrong with POST request, Please contact system administrator for further assistance::error";
                    return;
                }
                $table = "employees e";
                $selectData = "e.id AS ID,e.Phone,e.Mobile,e.CNIC,e.Perm_Address,e.Picture as pic,d.name as Designation,s.title as Shift, e.Name,e.Father_Name  AS FatherName,e.Phone AS Contact,e.JoinDate,e.BasicSalary, e.Pres_Address AS Address,e.IsEnabled";
                $where = array(
                    'e.id' => $ID, 'e.IsActive' => 1
                );
                $result = $this->Common_model->select_fields_where_like_join($table, $selectData,$where, TRUE);
                print json_encode($result,JSON_UNESCAPED_SLASHES);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

我想您在JavaScript中具有base_url()功能可以找到您网站的基本网址。 如果是这样,那么您可以使用

$("#EditImage").attr("src",base_url()+outputData.pic);

如果您在javascript中没有base_url()函数,可以在这里找到它: how to get the base url in javascript

答案 1 :(得分:0)

代替此

url: "'.base_url().'Employees/master_get_employees",

您可以在下面使用

url: "<?php echo site_url('Employees/master_get_employees'); ?>",

同样在服务器端,如果您只需要图像URL,则只需从$ result创建图像URL,将其存储在“ master_get_employees”函数中的$ img_path之类的变量中,然后只需

echo json_encode(['img_path'=>$img_path]);

要成功实现ajax,只需执行以下操作

$("#EditImage").attr('src',outputData.img_path);