Slim PHP返回的jQuery .submit()上传表单显示响应 [英] jQuery .submit() upload form display response returned by Slim PHP

查看:115
本文介绍了Slim PHP返回的jQuery .submit()上传表单显示响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于将文件上传到服务器的html表单.为简洁起见,我只展示了必要的调味料

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>

我使用jQuery.submit()提交表单:

I use jQuery.submit() to submit the form:

 $('#uploadform').submit();

业务逻辑是Slim PHP: $ app-> post('/upload','uploadFile'); ....

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();

       }
    }

问题:如果由于某种原因上传失败,则会引发异常,将用户带到PHP错误页面.如果上载无一例外地完成,则应用程序将重定向到主页.

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.

要求的是::如果上传成功,则应像现在一样将应用程序重定向到主页...但是,如果抛出任何异常,而不是转到PHP错误页面,应用程序应停留在上传页面上,并且id ='response'应显示异常.

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.

是否可以使用jQuery Submit()做类似的事情?

Is it possible to do anything like this with jQuery submit():

     $('#uploadform').submit(function(response){
           $('response').html(response);
     });

????

我知道JQuery上传文件插件会让生活更轻松...但这对我来说不是一个选择...

感谢任何指针!

有可能

推荐答案

您需要使用ajax,以下是使用ajax上传文件的示例

You need to use ajax here is example for file upload using ajax

使用jQuery.ajax发送multipart/formdata

没有ajax,您可以重定向用户并通过GET方法发送错误文本并将其放入响应标签

with out ajax you can redirect back user and send error text by GET method and put this text in response tag

这篇关于Slim PHP返回的jQuery .submit()上传表单显示响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆