开发者

Submitting Portion of Form to a Separate Action

开发者 https://www.devze.com 2023-03-08 15:59 出处:网络
In my shopping cart for my online store I\'d like there to be a checkbox to allow people to opt-in for my newsletter. But the email address for the newsletter needs to b开发者_运维百科e submitted to a

In my shopping cart for my online store I'd like there to be a checkbox to allow people to opt-in for my newsletter. But the email address for the newsletter needs to b开发者_运维百科e submitted to a separate form action than the shopping cart action.

Is it possible for me to submit part of a form to another action but still submit to the shopping cart form action and render wherever the shopping cart action would take me?


You can post your data using jquery:

This first ajax call will submit to your cart action:

function SubmitCart()
{
    $.ajax({
        type: 'POST',
        url: 'cart action url',
        data: { data1: $('#data1').val(), data2: $('#data2').val(), data3: $('#data3').val() },
        dataType: 'json',
        complete: function(XMLHttpRequest, textStatus) {
             // Action completed.
        }
    });
}

this other bit here will submit to your newsletter action:

function SubmitNewsletter()
{
    $.ajax({
        type: 'POST',
        url: 'newsletter action url',
        data: { email: $('#email_fields').val() },
        dataType: 'json',
        complete: function(XMLHttpRequest, textStatus) {
             // Action completed.
        }
    });
}

Now, you would have a submit button somewhere:

<input type="button" id="sumit_all" name="sumit_all" value="send" />
<input type="checkbox" id="subscribeNewsletter" name="subscribeNewsletter" />

and the jQuery script:

$("sumit_all").click(function() {
   SubmitCart();
   if ($('#subscribeNewsletter').is(':checked'))
   {
      SubmitNewsletter();
   }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号