javascript - page is getting refress after uploading a file -
i trying upload file using ajax function , file getting uploaded problem after few time page getting refresh data has gone. please me prevent page load.
my html code is:
<form> <input type="file" name="upl" id="upl" /><br /> <input type="submit" name="submit" id="submit" /> </form> <p id="res"></p>
my js file code is:
$("#upl").on('change',function(e){ e.preventdefault(); e.stoppropagation(); var file_data = $(this).prop('files')[0]; var form_data = new formdata(); form_data.append('file', file_data); $.ajax({ url: 'upload.php', // point server-side php script datatype: 'text', // expect php script, if cache: false, contenttype: false, processdata: false, data: form_data, type: 'post', success: function(php_script_response){ $("#res").append(php_script_response); // display response php script, if if(php_script_response){ return false; } } }); });
and php file code is:
<?php if ( 0 < $_files['file']['error'] ) { echo 'error: ' . $_files['file']['error'] . '<br>'; } else { if( move_uploaded_file($_files['file']['tmp_name'], 'uploads/' . $_files['file']['name']) ){ echo "uploaded"; } else{ echo "not uploaded"; } } ?>
please solve problem.
because havent added action form , want use ajax no need of form.
Comments
Post a Comment