tags:

views:

130

answers:

3

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!

+2  A: 

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/

ocdcoder
haha, oops, yea
ocdcoder
A: 

Will it work for you?

$('#myform').submit(function() {
$.post('mypage.php', serialized_form, 
    function(data) {
        ...
       $.post('http://example.org', serialized_form, ....);
    }, 'html'
);
return false;
}

<form id="myform" method="post">...</form>
a1ex07
No, as I need the form to actually complete its action (with the user being taken to the external site and such).
Wickethewok
I don't think he's trying to do two ajax requests...just one to his site and then actually have the form go to the other site
ocdcoder
A: 

If I understood your problem, then you need to your data to $url and still submit the form afterwards. Hope the following code works for you (not tested^^):

;(function($) {
    var sent = false;

    $('#myform').submit(function() {
        if(sent) {
            return true;
        }

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

    return false;
    });
})(jQuery);
x3ro
I tried something like this before, but the call to submit() would not work.
Wickethewok