我想在不加载新php页面的情况下提交表单。我的表单包含一个文件上传和文本区域字段。基本上,数据可以像博客一样,即大数据大小必须提交
我正在学习一些关于jquery ajax表单提交的教程
- 教程中没有任何内容显示如何上传文件。有人能帮我这个吗
- 既然使用了get方法,那么jqueryajax可以提交的数据大小是否有限制。如果是,我该如何处理
您可以使用iframe,这样就不必刷新当前页面。
许多人直接使用插件。但是您可以很容易地自己完成这项工作,并且具有AJAX请求的所有功能。
使表单提交到一个隐藏的iframe,该iframe附加了一个加载事件处理程序,因此在提交表单时,您有一个回调函数,该函数实际上包括服务器响应(加载后iframe的HTML)。
示例:
<form action="..." method="post" encrypt="application/x-www-form-urlencoded" target="workFrame" >
<input type="file" name="file" />
<input type="submit" />
</form>
<iframe id="workFrame" src="about:blank" style="display:none;"></iframe>
Javascript代码为:
(function () {
$('form').on('submit', function () {
//check if the form submission is valid, if so just let it submit
//otherwise you could call `return false;` to stop the submission
});
$('#workFrame').on('load', function () {
//get the response from the server
var response = $(this).contents().find('body').html();
//you can now access the server response in the `response` variable
//this is the same as the success callback for a jQuery AJAX request
});
});
正如您所知,限制是设置的,而不是浏览器。根据许多众所周知的指令,例如在Apache中,这一段定义了参数LimitRequestBody。此参数可以取值从0字节到2 GB。在PHP中,还有一个名为post_max_size的指令。指定POST请求的大小。