2

I looked through the site for answers to this, but nothing's spot on to what I need (this is close, except it doesn't actually submit the form: Prevent form redirect OR refresh on submit?).

I'm trying to incorporate a mailing list sign-up (code borrowed from a sign-up page hosted on ReverbNation) to a website.

The form submits properly, but the signee is redirected to a hideously rendered page on ReverbNation's site. I cannot modify their script and don't think there's an API I can use to keep things tidy.

Is there a way I can submit the form in the background, without the user being redirected?

Community
  • 1
  • 1
Kate
  • 120
  • 3
  • 13

3 Answers3

2

If you're posting to the same domain, you can use an AJAX post. However, it seems you're trying to POST to different domain, so the browser's same origin policy will prevent you from doing so (http://en.wikipedia.org/wiki/Same_origin_policy). (JSONP can get around this but it doesn't work for POST)

Another way to get around this is to have your server do the POST and tunnel the response back to your page.

<form id='yourForm' action="" onsubmit="javascript: doPostToTunnelPage(); return false;">
    <!-- inputs...-->
</form>

Make sure to return false, or your page will be redirected.

Saurav
  • 3,096
  • 3
  • 19
  • 12
  • Saurav: Yes, I knew I couldn't use AJAX, since it's external. I don't need a response from the submission, but a clarification on how to have my server do the POST in the background would be immensely helpful! – Kate Feb 24 '11 at 00:21
  • Sure thing. Which language are you using? – Saurav Feb 24 '11 at 00:24
  • I'm using PHP and jQuery. Thank you for your help! – Kate Feb 24 '11 at 00:27
2

Here's an example in PHP for tunneling a POST.

//set POST variables
$url = 'http://domain.com/url-to-post-to';
$fields = array(
            // Add the fields you want to pass through
            // Remove stripslashes if get_magic_quotes_gpc() returns 0.
            'last_name'=>urlencode(stripslashes($_POST['last_name'])),
            'first_name'=>urlencode(stripslashes($_POST['first_name'])),
            'email'=>urlencode(stripslashes($_POST['email']))
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));

// returns the response as a string instead of printing it
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

echo $result;
Saurav
  • 3,096
  • 3
  • 19
  • 12
  • 1
    Thank you! I ended up using your PHP tunnel plus the following jQuery: `jQuery('#mailinglist').submit(function (e) { e.preventDefault(); var dataString = 'email=' + jQuery('#mailinglist #email').val(); jQuery.ajax({ type: "POST", url: "path_to/tunneling.php", data: dataString, success: function() { jQuery('#mailinglist #email').val(function(index, value) { return 'thanks!'; }); } }); return false; });` – Kate Feb 24 '11 at 01:25
0

in my understand, you need send a form without redirect?

consider my example

$(function() {
    $('#myForm').submit(function (e) {
        e.preventDefault();

        $.ajax({ /* params to send the form */ });

        return false;
    });
});

IIT it will work just because of the e.preventDefault.

If this method is called, the default action of the event will not be triggered. 

See jQuery documentation here for more information.

Hope it help you

Kim Tranjan
  • 4,521
  • 3
  • 39
  • 38
  • Kim: yes, but sendForm(); throws an error because it's not defined. Can you please explain what the sendForm() function should contain? – Kate Feb 24 '11 at 00:37
  • Kim: I cannot use AJAX as this POST needs to happen on an external site. – Kate Feb 24 '11 at 00:41
  • Hmm.. I understood your scenario. Check out the another post of @Saurav, it may help you. Sorry my answer not help you. – Kim Tranjan Feb 24 '11 at 00:43