无法使用 jQuery 提交表单

发布于 2024-12-14 02:27:18 字数 2074 浏览 0 评论 0原文

我有这个表格

<form id="frm_main" action="#" method=POST>
 <input type=hidden name="MAX_FILE_SIZE" value="100000" />
 <input id='file' name="file" type="file">
 &nbsp;&nbsp;
 <input type=submit id='btn_import' name='btn_import' value='Importar' />
 <input type=hidden id="uploadResponseType" name="mimetype" value="html" />
 <input type=hidden id="func" name="func" value="upload" />
 <div id="uploadOutput"></div>
 </form>

所以,当我尝试发布上传的文件时,问题就出现了。如果我使用 ajaxSubmit 函数,表单会被提交,但不会返回到页面;如果我使用 $.ajax 函数,它不会发送上传文件。问题是我需要返回同一页面,因为我必须对文件内容执行一些操作。我已经尝试了很多组合,但仍然得到相同的结果。处理提交的代码看起来像这样

$( '#frm_main' ).bind( 'submit', function( e ) {
// e.preventDefault(); // <-- important
$( this ).ajaxSubmit({
target: '#uploadOutput', // <-- div container
type: "POST", // <-- override, just in case.
url: "/process.php", // <-- server-side handler
data: "func=upload", // <-- parameter for post purpouse
beforeSubmit: function(a,f,o) {
o.dataType = $('#uploadResponseType').val();    // should be 'html'.
$('#uploadOutput').html('Submitting...');
},
success: function(data) {
var $out = $('#uploadOutput');
$out.html('Form success handler received: <strong>' + typeof data + '</strong>');
$out.append('<div><pre>'+ data +'</pre></div>');
}
});
return false;
});

我已经尝试过下一个代码

$('#frm_main').bind('submit', function() {
var formdata = $(this).serialize();
$.ajax({
    url:    '/process.php',
    data:   formdata,
    dataType: 'html', 
    success: function(data){
        var $out = $('#uploadOutput');
        $out.html('Form success handler received: <strong>' + typeof data + '</strong>');
    }
});
return false;
});

并且 process.php 代码看起来像这样

switch($_POST['func']) {
  case 'upload' : 
    $output = upload_file ();
    echo $output;
   break;
 default : 
   echo "<BR/>INVALID";
   break;
}
?>

有人可以帮我解决这种情况吗?任何帮助将不胜感激

I've this form

<form id="frm_main" action="#" method=POST>
 <input type=hidden name="MAX_FILE_SIZE" value="100000" />
 <input id='file' name="file" type="file">
   
 <input type=submit id='btn_import' name='btn_import' value='Importar' />
 <input type=hidden id="uploadResponseType" name="mimetype" value="html" />
 <input type=hidden id="func" name="func" value="upload" />
 <div id="uploadOutput"></div>
 </form>

So, the problem became when I try to post the upleaded file. If I use ajaxSubmit function the form is submited but it doesn't return to the page; and if I use $.ajax function, it doesn't sent the uploeade file. The thing is that I need to return to the same page because I've to do some stuff with the file content. I've already tried lot of combinations but I'm still having the same results. The code to handle submit look like this

$( '#frm_main' ).bind( 'submit', function( e ) {
// e.preventDefault(); // <-- important
$( this ).ajaxSubmit({
target: '#uploadOutput', // <-- div container
type: "POST", // <-- override, just in case.
url: "/process.php", // <-- server-side handler
data: "func=upload", // <-- parameter for post purpouse
beforeSubmit: function(a,f,o) {
o.dataType = $('#uploadResponseType').val();    // should be 'html'.
$('#uploadOutput').html('Submitting...');
},
success: function(data) {
var $out = $('#uploadOutput');
$out.html('Form success handler received: <strong>' + typeof data + '</strong>');
$out.append('<div><pre>'+ data +'</pre></div>');
}
});
return false;
});

And I've already tried with next code

$('#frm_main').bind('submit', function() {
var formdata = $(this).serialize();
$.ajax({
    url:    '/process.php',
    data:   formdata,
    dataType: 'html', 
    success: function(data){
        var $out = $('#uploadOutput');
        $out.html('Form success handler received: <strong>' + typeof data + '</strong>');
    }
});
return false;
});

And process.php code looks like this

switch($_POST['func']) {
  case 'upload' : 
    $output = upload_file ();
    echo $output;
   break;
 default : 
   echo "<BR/>INVALID";
   break;
}
?>

Can somebody help me with this situation, please? Any help will be gratefully appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2024-12-21 02:27:18

上传文件不需要form enctype="multipart/form-data" 吗?

Don't you need form enctype="multipart/form-data" to upload files?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文