![](/img/trans.png)
[英]PHP: Grab an image from a stream (in an img tag) but if it's not there, i don't want the img tag written. How?
[英]I want to show image in img tag when ajax is call in 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);
}
}
}
我想您在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,將其存儲在變量中,例如“ img_path”在“ master_get_employees”函數中,然后
echo json_encode(['img_path'=>$img_path]);
在ajax成功中,只需執行以下操作
$("#EditImage").attr('src',outputData.img_path);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.