2

In my app, a user must be signed in to submit form info.

After a user clicks on the form submit button, my jQuery checks if a user is signed in.

If not signed in, then an error message pops up, requesting sign in/up. I can now successfully stop the default action (submit).

However, how do I also allow the default action if the user is already signed in? With my current code, the default action is also blocked if the user is signed in.

Here's my code:

jQuery('.member-only').click(function(event) {
    var $element = jQuery(this);
    var SignedIn;

    jQuery.ajax({
        type: 'POST',
        url: '/ajax/member',
        dataType: 'json',
        success: function(data) {
            var $err = jQuery('<div></div>')
                        .addClass('member-check')
                        .html(data.msg)
                        .css('left', $element.position().left);
            SignedIn = data.SignedIn;

            if (!(data.SignedIn)) { // not signed in
                $element.after($err);
                $err.fadeIn('slow');
                return false;
            }
        }
    });

    jQuery('.member-check').live('click', function() {
        jQuery(this).fadeOut('slow', function() {jQuery(this).remove(); });
    });

    if (!SignedIn) {
        event.preventDefault();
        event.stopImmediatePropagation();
        return false; // block default submit
    }
});

Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JMan
  • 1,815
  • 5
  • 21
  • 34
  • 9
    Require that the user be logged in before they can even see the form? – Mark Tomlin Aug 02 '10 at 03:47
  • @Mark Tomlin, eight seconds faster than me... +1, sir =) – David Thomas Aug 02 '10 at 03:47
  • No - I want the user to see the form, enter info, and then if they are not signed up, show them the sign up message. – JMan Aug 04 '10 at 05:32
  • 2
    That being the case, I hope you'll maintain their entries somewhere so they don't have to re-enter them after signing up... Nothing's more annoying than trying something, being told you have to register, then having to do it all over again. – Dan J Aug 04 '10 at 19:09
  • @djacobson - any recommendations on how to accomplish that? – JMan Aug 04 '10 at 21:10
  • Depends on how you're accomplishing the signup - I'm assuming you're redirecting to a new page. Using PHP on the server-side, right? Well, you have options ranging from storing the user's form entries in a Session object, to actually persisting them in a DB. – Dan J Aug 04 '10 at 22:47
  • Just wanted to chime in to say that You should also be doing a check server-side to make sure that data being sent is from signed in users. – TCCV Aug 05 '10 at 02:39
  • This should not depend on Javascript, since Javascript can be turned off (e.g. noScript). You can do all this with PHP alone. – Peter Ajtai Aug 05 '10 at 20:20

7 Answers7

2

You need to let your JS function return false; to block the event's default action.

However, this doesn't cover users who have JS disabled or are capable to spoof it. So you should handle this gracefully in the server side as well :)


Update: As per your update, please add

alert(typeof Signedin);
alert(Signedin);

right before if(!Signedin) and tell what you get for both cases. It might be of the wrong type and/or value which is causing that you're always entering the if block and thus always returning false.

For example, a type of undefined will always cause !Signedin to evaluate true. You'd like it to be a boolean type all the time with values true or false.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Sorry, I don't understand. I tried the following and it didn't prevent the done/submit action from being called. jQuery('.member-only').click(function() { var element = jQuery(this); jQuery.ajax({ type: 'POST', url: '/ajax/member', dataType: 'json', success: function(data) { var err = ;// error msg if (!(data.SignedIn)) // not signed in { element.after(err); err.fadeIn('fast'); return FALSE; } // otherwise just continue... – JMan Jul 31 '10 at 16:54
  • 1
    JS is case sensitive. The right keyword is `false`, not `FALSE`. You also need to ensure that this is always been returned regardless of the condition or ajax outcome. I.e. just put at the very bottom of the function. – BalusC Jul 31 '10 at 17:02
  • 1
    Got it. I needed to add a return false outside of my ajax call, as well. Thanks. – JMan Aug 02 '10 at 18:15
1

This is a Moo question. Your not loged in user should have never seen a form that he can't submit in the first place.

Fix your PHP so as to not write a form that a non-logged in user can't submit.

Chase Wilson
  • 1,477
  • 1
  • 12
  • 19
0

See if there is any user logged in . keep a flag for it. If flag is not set just disable the submit button . or just set the form action part using jquery only if flag is set.

Hacker
  • 7,798
  • 19
  • 84
  • 154
  • How do I dynamically disable the submit button? I also need it to be re-enabled once the user signs in. – JMan Jul 31 '10 at 16:37
0

Use event.preventDefault(); in your event handler. Return false only works in some cases (it's more of a workaround).

http://api.jquery.com/event.preventDefault/

https://developer.mozilla.org/en/DOM/event.preventDefault

balupton
  • 47,113
  • 32
  • 131
  • 182
0
$(form_id).submit(function(){ //triggered when user submits form
  var signed_in = check_if_user_is_signed_in(); //checking
  if(signed_in){ //singed in
    //Do stuff
    return true; //submit form
  } else{ //user not signed in
    //Do stuff
    return false; //prevent form from being submitted
  }
})
azram19
  • 1,787
  • 1
  • 9
  • 6
0

I would also add an ajax request to check if user is logged in if your website often is opened in multiple windows.

vlad b.
  • 695
  • 5
  • 14
0

Please review my modifications to your code. Let me know the datatype of the returned data.SignedIn; I added a console.log to return it to firebug.

My example takes action on the document being ready, as opposed to waiting for the user to interact, thus preventing the usability problem of showing the user that an ansynchronous call is happening in the background (the ajax spinner):

$(document).ready(function($){
    var memberStatus;
    jQuery.ajax({
        type: 'POST',
        url: '/ajax/member',
        dataType: 'json',
        success: function(data) {
console.log(data.SignedIn) //I need to know what format this is returned (Boolean: True/False?)
memberStatus = data.SignedIn;
        }
    });
   if (memberStatus) { //if true, that means they are a member
      //draw member box
   } else {
      $("#submitButtonID").attr('disabled',true);
   }
});
Bob Gregor
  • 1,163
  • 9
  • 13