0

I want to show an image in img tag. when ajax is called in CodeIgniter. this is the code which receives the data from the database and displays it in a bootstrap model. but the main problem is that I want to show an image in img tag but it not showing.

        $(".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");
        }
        }
        }
        });
        });

And This is the Img tag where I want to display image.

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

And this is the function through which data is fetched from the database

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);
            }
        }
    }
Madhuri Patel
  • 1,270
  • 12
  • 24
John Merry
  • 31
  • 10
  • you are aware that `base_url()` is a server side function right? did you create a js implementation? further "it is not displaying" isn't helpful. what is your json response? what is happening? any console errors? – Alex Nov 03 '18 at 05:48

2 Answers2

0

I guess you have base_url() function in javascript to find base url of you site. If so then you can use

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

If you don't have base_url() function in javascript you can find it here: how to get the base url in javascript

Atal Prateek
  • 541
  • 3
  • 7
0

instead of this

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

You can use below,

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

Also at server side if you need only image url then just create image url from $result, store it in variable like $img_path in "master_get_employees" function and then just

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

And in ajax success just do below

$("#EditImage").attr('src',outputData.img_path);
kishan
  • 138
  • 1
  • 3
  • 11