我想在img
标记中显示一个图像。当在CodeIgniter中调用ajax时。这是从数据库接收数据并将其显示在引导程序模型中的代码。但主要问题是我想在CCD_ 2标签中显示图像,但它没有显示。
$(".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);
}
}
}
我猜您在javascript中有base_url()
函数来查找您网站的基本url。如果是这样,那么你可以使用
$("#EditImage").attr("src",base_url()+outputData.pic);
如果你在javascript中没有base_url()
函数,你可以在这里找到它:如何在javascript 中获取基本url
而不是此
url: "'.base_url().'Employees/master_get_employees",
您可以使用以下
url: "<?php echo site_url('Employees/master_get_employees'); ?>",
同样在服务器端,如果您只需要图像url,那么只需从$result创建图像url,将其存储在变量中,如"master_get_employes"函数中的$img_path,然后只使用
echo json_encode(['img_path'=>$img_path]);
在ajax的成功中,只需低于
$("#EditImage").attr('src',outputData.img_path);