php - File upload with jQuery and ajax not working -
file uploading working in program after refreshing page.
javascript
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="<?php echo base_url()?>assets/scripts/ajaxfileupload.js"></script> <script type="text/javascript"> function ajaxfileupload(){ $("#loading") .ajaxstart(function(){ $(this).show(); }) .ajaxcomplete(function(){ $(this).hide(); }); $.ajaxfileupload ( { url:'<?php echo site_url()?>/welcome/doupload', secureuri:false, fileelementid:'filetoupload', datatype: 'json', success: function (data, status){ if(typeof(data.error) != 'undefined'){ if(data.error != ''){ $("#info").html(data.error); } else { $("#info").html(data.msg); } } }, error: function (data, status, e){ $("#info").html(e); } } ) return false; } </script>
html
<form name="form" action="" method="post" enctype="multipart/form-data"> <input id="filetoupload" type="file" size="45" name="filetoupload" class="input"> <button class="button" id="buttonupload" onclick="return ajaxfileupload();">upload</button> </form>
controller
public function doupload() { $error = ""; $msg = ""; $config['upload_path'] = './assets/'; $config['allowed_types'] = 'doc|xls|ppt|pdf|rar|jpg'; $config['max_size'] = '1000'; $this->load->library('upload', $config); $this->upload->display_errors('',''); if ( ! $this->upload->do_upload("filetoupload")) { $error = $this->upload->display_errors(); } else { $msg = "success"; } echo "{"; echo "error: '" . $error . "',n"; echo "msg: '" . $msg . "'n"; echo "}"; }
my guess buttons of "submit" type default when in form tag.
try set type "button" :
<button type="button" class="button" id="buttonupload" onclick="return ajaxfileupload();">upload</button>
Comments
Post a Comment