I have a html form used to upload file to the server. For brevity, I have only shown the essential peices
<form id='uploadform' method='post' enctype='multipart/form-data' action='index.php/upload'>
<input name='myFile' id='myFile' type='file'/>
</form>
<input type='button' id='upload' value='Upload'/>
<div id='response'></div>
I use jQuery.submit() to submit the form:
$('#uploadform').submit();
Business logic is Slim PHP :
$app->post(‘/upload’, ‘uploadFile’);
….
function uploadFile(){
try{
// if success uploading
$app->redirect('/main-page');
}catch(Exception $e){
// if error
echo $e->getMessage();
}
}
Issue: If the upload fails for some reason, an exception is thrown, user is taken to a PHP error page. If upload was completed without exception, the application is redirected to main page.
What is required is: if the upload succeeds the application should be redirected to main-page as it does now…but if there was any exception thrown, instead of going to PHP error page, the application should stay on upload page and with id = ‘response’ should display exception.
Is it possible to do anything like this with jQuery submit():
$('#uploadform').submit(function(response){
$('response').html(response);
});
????
I know JQuery upload file plugins would make life easier…but that is not an option for me…
Thanks for any pointers!
Is it possible
You need to use ajax here is example for file upload using ajax
Sending multipart/formdata with jQuery.ajax
with out ajax you can redirect back user and send error text by GET method and put this text in response tag