views:

161

answers:

2

Hello

I would like to set a cookie when a user submits a form.

I am working with some legacy code that sets a cookie based on a user checking a checkbox, but would like this to simply set the cookie when a user submits a form.

Here is what i have, which works when a checkbox is checked:

$().ready(function()
{
    $('.application-form').submit(function(e)
    {
        e.preventDefault();

        if ($('.application-form input[name=marketing]').is(':checked'))
        {
            $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
        }
    });
});

This works fine, but i don't want the cookie set when checking the "marketing" input, rather just when the form submits. Like this:

$().ready(function()
{
    $('.application-form').submit(function(e)
    {
        e.preventDefault();

        if ($('.application-form').submit();)
        {
            $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
        }
    });
});

However, this does not set the cookie successfully, any ideas would be great,thanks.

+2  A: 
$(document).ready(function() {
  $('.application-form').submit(function(e) {
    e.preventDefault();
    $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
  });
});

You don't need the if condition there, because it seems to be testing whether the form has been submitted or not, and you already know that it has, because you're in the form's submit event handler.

GlenCrawford
Great. This sets the cookie successfully and submits the form, however, the page does not reload nor does it go to it's "thank you" page, whereas when I set the cookie using my previous method, this would happen. The page appears to do nothing, even though it sets the cookie, submits to the database and sends an email - it works, but it seems something is connected to the .submit() where i am setting the cookie, that affects the way the form behaves post submit, any ideas? thank you
Dave
I'm a little confused. After setting the cookie, do you want the form to submit normally to the page specified in its "action" attribute?Does removing the "e.preventDefault();" make any difference?
GlenCrawford
Yes that's correct, I would like it to submit normally. Removing the "e.preventDefault();" makes the form behave as I would like, so many thanks. Apologies but as i said this is legacy code, and i wasn't fully aware what "e.preventDefault();" does - and i'm not really - does it do as it's name would suggest? Stop a browsers default behavior from occurring, in this, the form submit? Thank you.
Dave
Yes, "e.preventDefault();" stops the browser from performing the default action, in this case, that's submitting the form. From the jQuery documentation: "If this method is called, the default action of the event will not be triggered". By the way, would appreciate an upvote and answer accept if this has been the correct answer, that way, other people stopping by with the same (or similar) problem will be able to locate the correct answer.
GlenCrawford
Many thanks that's very helpful. i have accepted the answer, but i need a 15 reputation to upvote - I am new to this.
Dave
+1  A: 

You don't need to check for submit return value in if condition.

$().ready(function()
{
    $('.application-form').submit(function(e)
    {
        e.preventDefault();

        $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
    });
});
Krunal
Thank you, this works, but highlights some other issues i am thinking about now (see above).
Dave
@Dave - would appreciate if you upvote as it works for you.
Krunal