我正试图将用户个人资料图片的数据插入数据库。该功能工作正常,但问题是通知用户的消息没有显示任何内容。我已经附上了下面的控制器代码,因为我正在使用代码点火器。
function fresherImageStore()
{
if (isset($_FILES["image_file"]["name"]))
{
$user = $this->session->userdata('fresher_id');
$config['upload_path'] = './uploads/fresherimages';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image_file'))
{
$error = $this->upload->display_errors();
echo json_encode(array('msg' => $error, 'success' => false));
}
else
{
$user = $this->session->userdata('fresher_id');
$data = array('upload_data' => $this->upload->data());
$data1 = array(
'user' => $user,
'name' => $data['upload_data']['file_name']
);
$query = $this->db->get_where('images',array('user'=>$user));
if ($query->num_rows() > 0) {
$this->db->set('name',$data['upload_data']['file_name'])
->where('user',$user)
->update('images');
$arr = array('msg' => 'Your image has been uploaded successfully', 'success' => true);
}
else
{
$this->db->insert('images',$data1);
$getId = $this->db->insert_id();
$arr = array('msg' => 'Image has not uploaded successfully', 'success' => false);
if ($getId) {
$arr = array('msg' => 'Image has been uploaded successfully', 'success' => true);
}
echo json_encode($arr);
}
}
}
}
那么这就是html
<form method="POST" id="upload_form" enctype="multipart/form-data">
<div class="col-md-7">
<h3> Upload your photo</h3></br>
<div id="divMsg" class="alert alert-success" style="display: none">
<span id="ms"></span>
</div>
<input type="file" name="image_file" multiple="true" accept="image/*" id="finput" onchange="readURL(this);"></br></br>
<button class="btn btn-success">Submit</button>
</div>
<div class="col-md-5"></div>
最后是jquery函数
$(document).ready(function(){
$('#upload_form').on('submit', function(e){
e.preventDefault();
if($('#image_file').val() == '')
{
alert("Please Select the File");
}
else
{
$.ajax({
url:"<?php echo base_url(); ?>images/seekerImageStore",
method:"POST",
data:new FormData(this),
contentType: false,
cache: false,
processData:false,
dataType: "json",
success:function(res)
{
console.log(res.success);
if(res.success == true){
$('#blah').attr('src','//www.tutsmake.com/ajax-image-upload-with-preview-in-codeigniter/');
$('#msg').html(res.msg1);
$('#divMsg').show();
}
else if(res.success == false){
$('#msg').html(res.msg);
$('#divMsg').show();
}
setTimeout(function(){
$('#msg').html('');
$('#divMsg').hide();
}, 3000);
}
});
}
});
您的jQuery代码正试图将消息附加到id为msg
的元素上,但消息应该指向的span
标记被称为ms
。
如果你查看浏览器的控制台,你会看到错误。您指向的是一个不存在的ID
您不会收到任何关于"成功"的消息,因为当遇到这种情况时,您不会发送任何json。
只需从嵌套if/else:中取出takeecho json_encode($arr);
if ($query->num_rows() > 0) {
$this->db->set('name',$data['upload_data']['file_name'])
->where('user',$user)
->update('images');
$arr = array('msg' => 'Your image has been uploaded successfully', 'success' => true);
}
else
{
$this->db->insert('images',$data1);
$getId = $this->db->insert_id();
$arr = array('msg' => 'Image has not uploaded successfully', 'success' => false);
if ($getId) {
$arr = array('msg' => 'Image has been uploaded successfully', 'success' => true);
}
}
echo json_encode($arr);
然后在您的视图中(ajax成功(,您可以输出如下消息:
$('#divMsg').show().find('span').html(res.msg);