How to stop sending an HTML form if the user is not signed up, but allow sending if the user is logged in?

In my application, the user must be logged in to submit form information.

After the user clicks the submit button, my jQuery checks if the user is signed.

If you are not logged in, an error message will appear asking you to login / up. Now I can successfully stop the default action (send).

However, how can I also enable the default action if the user is already subscribed? With my current code, the default action is also blocked if the user is logged in.

Here is 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 } }); 

Thank.

+2
javascript jquery php
Jul 31 '10 at 6:10
source share
7 answers
 $(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 } }) 
0
Aug 04 '10 at 10:41
source share

You need to enable the JS function return false; to block the default action for the event.

However, this does not apply to users who have JS disabled or they can trick it. So you should handle this gracefully and server side :)




Update : according to your update, add

 alert(typeof Signedin); alert(Signedin); 

right before if(!Signedin) and tell us what you get for both cases. This may be the wrong type and / or value, which causes you to always enter the if block and thus always return false.

For example, an undefined type always evaluates !Signedin true . You want it to be a boolean type all the time with true or false values.

+2
Jul 31 '10 at 6:13
source share

This is a Mu question. Your user who is not registered on the network should never see a form that he cannot submit in the first place.

Correct your PHP so as not to write a form that the user cannot complete.

+1
Aug 05
source share

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

0
Jul 31 '10 at 6:17
source share

Use event.preventDefault(); in the event handler. Returning false only works in some cases (this is more of a workaround).

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

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

0
Aug 02 2018-10-12T00:
source share

I would also add an ajax request to check if the user is registered if your site often opens in multiple windows.

0
Aug 4 '10 at 18:59
source share

Read my code changes. Let me know the data type of the returned data. SignedIn; I added console.log to return it to firebug.

In my example, actions are taken to ensure that the document is ready, in contrast to waiting for user interaction, thereby preventing a usability problem showing the user that a synchronous call is happening in the background (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); } }); 
0
Aug 05 2018-10-10T00:
source share



All Articles