HTML
<form method="POST" id="formID" style="margin:0 auto" enctype="multipart/form-data"> <label for="uattachment">Attach File <span class="red">*</span></label> <input type="file" name="file" id="uattachment" value="" /> <input type="submit" name="submit" id="submit"> </form>
$('#formID').submit(function(){
var file = document.getElementById('uattachment').value;
if (file.indexOf('/') > -1) file = file.substring(file.lastIndexOf('/') + 1);
else if (file.indexOf('\\') > -1) file = file.substring(file.lastIndexOf('\\') + 1);
$.each($('#uattachment').prop("files"), function(i, file) {
formData.append('file-'+i, file);
});
$.ajax({
cache: false,
async: false,
dataType: 'JSON',
type: 'POST',
url: 'b.php',
data: formData,
success: function(data, textStatus, jqXHR){
alert("msg : "+ data.msg);
},
error: function(jqXHR, error, thrownError){
if (jqXHR.readyState == 0 || jqXHR.status == 0) {
return false; //Skip this error
}else{
alert("Error : \nReady state = "+ jqXHR.readyState +"\nStatus = "+ jqXHR.status +"\nResponse = "+ jqXHR.responseText);
}
},
complete: function(jqXHR, textStatus){
alert("Status : "+ textStatus +"\nStatus desc : "+jqXHR.readyState);
}
});
here is the PHP code
$files = $_FILES['file'];
echo json_encode( array('msg' => $files[0]['name'] ) );

New Topic/Question
Reply


MultiQuote





|