2013-09-26 81 views
-1

我想知道的是,为什么我的页面,而我使用AJAX上传图片获取刷新,我试图调试问题,但did'nt发现,Ajax图片上传刷新页面?

这里是控制器的功能,

public function uploadImage() { 
    if(isset($_FILES['header_image'])) { 
     $config['upload_path'] = 'public/images/global/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['file_name'] = 'b_'.random_string('alnum', 5); 
     $this->upload->initialize($config); 

     if($this->upload->do_upload('header_image')) { 
      echo json_encode(array('status'=>"1", 'image_url'=>$this->upload->file_name)); 
     } else { 
      $upload_error = array('error' => $this->upload->display_errors()); 
      echo json_encode(array('status'=>"0",'upload_error'=>strip_tags($upload_error['error']))); 
     } 
     exit; 
    } 
    $this->template->render(); 
} 

虽然我的看法是uploadImage.php模样,

$(document).ready(function() { 
$('#header_image').on('change', function(){ 
    $("#frm_global").ajaxForm({ 
     success: function(data) { 
      res = $.parseJSON(data); 
      if(res.status == 1){ 
       var html = "<img src='<?=base_url();?>path/"+res.image_url+"' class='preview' width='100' height='100' />"; 
        html+= "<input type='hidden' id='image_url' name='image_url' value='"+res.image_url+"' >"; 
        $("#preview").html(html); 
        $("#frm_global").ajaxFormUnbind(); 
      } 
      if(res.status == 0){ 
       $("#frm_global").ajaxFormUnbind(); 
      } 
     } 
    }).submit(); 
}); 
}); 

<form name="frm_global" id="frm_global" enctype="multipart/form-data"> 
<div id="preview" style="float:left"> 
</div> 
<div style="float:left"> 
    <div style="margin-top:25px"> 
     <input type="file" name="header_image" id="header_image"/> 
     <input style="margin-left:100px" onclick="" type="image" id="upload_header" src="any path" /> 
    </div> 
</div> 
</form> 
+0

没有您的图像上传或不? –

+0

是的,图像获取上传 –

回答

0

对不起,我想我没有正确地得到您的问题。

但提交你可以尝试这样的

$("#theForm").ajaxForm({ 
url: 'server.php', 
type: 'post', 
success:function(data) 
{ 
//do what you want to 
} 
}); 

可能它可以帮助你

+0

表单确实,nt提交,如果我删除.submit()在末尾 –

+0

你指定的网址和类型? –

+0

是的,我提到那些 –