提交之前jQuery表单文章 [英] jQuery form post before submit

查看:112
本文介绍了提交之前jQuery表单文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有表单的网页,需要发布到外部网址。不过,我也需要将这些信息发送到当前页面(mypage.php)。出于安全原因,我不能只发布到mypage.php并使用cURL通过PHP发布到外部网站 - 表单必须直接提交到外部网站。



这个代码可以在mypage.php上找到,并且不起作用(我假设myform的提交并不等待发布):

  $('#myform')。submit(function(){
$ .post('mypage.php',serialized_form,
function(data){
...
},'html'
);
}

...

< form id =myformaction =http: //example.orgmethod =post>
...
< / form>

做这种事情的最佳方法是什么?

谢谢!

解决方案

您可以进行$ .post()同步,这会阻止javascript(和站点)继续执行直到它返回。



<如果你使用 $。ajax({type:'post',async:false})(加上你的ot她的参数)应该做的伎俩。



您可以阅读更多信息@ http://api.jquery.com/jQuery.ajax/


I have a page with a form on it that needs to post to an external URL. However, I also need this information to be sent to the current page (mypage.php). For security reasons, I cannot just post to mypage.php and use cURL to post via PHP to the external site - the form has to submit directly to the external site.

This code would be found on mypage.php and does not work (I assume that the submit of myform is not waiting for post):

$('#myform').submit(function() {
    $.post('mypage.php', serialized_form, 
        function(data) {
            ...
        }, 'html'
    );
}

...

<form id="myform" action="http://example.org" method="post">
...
</form>

What is the best way to do something like this?

Thanks!

解决方案

you can make the $.post() sync, which would block the javascript (and site) from continuing until its returned.

If you use $.ajax({type: 'post', async: false}) (plus your other params) that should do the trick.

You can read more info @ http://api.jquery.com/jQuery.ajax/

这篇关于提交之前jQuery表单文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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