views:

170

answers:

3

Hello

I have a sign up form that is displayed to all new site visitors. If a user fills out the form, the next time they visit the site, I would like to display a "welcome back" message where the form would usually sit.

I am trying to do this via the jquery cookie plugin (http://plugins.jquery.com/project/Cookie).

My form would look like this:

<div id="sign_up_form_wrapper"><form id="sign_up" action="" method="POST" name="form">
<input type="checkbox" name="checkbox" id="checkbox" value="1">&nbsp; I accept the terms and conditions</a>
<br /><br /><input type="submit" value="ENTER">
</form></div>

And I am setting my cookie here:

<script type="text/javascript" language="javascript">
$().ready(function()
{
    $('#sign_upm').submit(function(e)
    {
        e.preventDefault();

        if ($('#sign_up input[name=checkbox]').is(':checked'))
        {
            $.cookie('agreed_to_terms', '1', { path: '/', expires: 999999 });
        }
    });
});
</script>

That will set the cookie when a user has checked the box, but now I need to do somehting like this:

if the cookie has been set, do this:

<div id="sign_up_form_wrapper">
<p>Welcome back, John</p>
</div>

otherwise do this:

<div id="sign_up_form_wrapper">
<!-- full form code here -->
</div>

Any ideas or pointers would be very appreciated, thanks.

A: 

Just get the cookie and check if it is set.

var $signupFormWrapper = $('<div />');

if ( 1 == $.cookie(agreed_to_terms) )
{ 
     var $message = $('<p />').html('Welcome back, John');
} else {
     var $message = $('<p />').html('<form>Your full form</form>');
}

$signupFormWrapper.append($message);

Do this where you want depending on the event which has to be triggered

Boris Guéry
Thank you, this looks like it will do the trick.However, should this block be wrapped in javascript tags? When I do this, it displays the full HTML as text rather than executing the conditonal.Apologies but my js is very limited.Thanks
Dave
You have to do it in js, I updated the code to create the message and wrap it in the div, then append it where you want in the body for example.
Boris Guéry
Many thanks, but Jeffery To's solution seemed to work out better
Dave
A: 

Yes. Add it just over

});
</script>
dHahn
Thank you.I am taking these responses to mean I would do this:<script type="text/javascript">if ( 1 == $.cookie(rspca_signed_up) ){ <div id="sign_up_form_wrapper"> <p>Welcome back, John</p> </div>} else { <div id="sign_up_form_wrapper"> <!-- full form code here --> </div> }</script>But when I do this, I again can't get the conditional to work, just get the HTML output as text.
Dave
A: 

Boris Guéry is close:

HTML:

<div id="sign_up_form_wrapper">
<!-- full form code here -->
</div>

JavaScript:

$(function () {
    if ($.cookie('agreed_to_terms') == 1) {
        $('sign_up_form_wrapper').html('<p>Welcome back, John</p>');
    }
});
Jeffery To