Jump to content

Hiding HTML form if user is logged in


ryanmetzler3
Go to solution Solved by scootstah,

Recommended Posts

I have a form that requires, name, email, and a comment. There is then a submit button to send it all to ajax. If the user is already logged in, I redefine the value of the name field with a php variable that is stored in their session. This way a logged in user would not need to type their name. This code shows that: 

<p>Add a Comment</p>
     <form id="addCommentForm" method="post" action="">
	<label for="name">Your Name</label>
	<input type="text" name="name" id="name" />	
	<label for="email">Your Email</label>
	<input type="text" name="email" id="email" />    
        <label for="body">Comment Body</label>
        <textarea name="body" id="body" cols="20" rows="5"></textarea>

         //checks if user is logged in
         //if they are, redefine the value of "name"
        <?php if($username && $userid): ?>
    	<input type="hidden" name="name" id="name" value="<?php echo htmlspecialchars($username) ?>" />
    	<?php endif; ?>

        <input type="submit" id="submit" value="Submit" />
    </form> 

Since a logged in user no longer has to type their name, I want to visibly remove the name field if they are logged in. So I was thinking something like this: 

//check to see if user is logged in
//if they aren't, still display the name field
<?php if(!$username && !$userid): ?>
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />	
<?php endif; ?>

This way the name field will only show if the user is NOT logged in. Unfortunately when I do this, it makes the form fail. The submit button does not work when I put that if-statement in there. The name field is still defined, even though its not showing so I thought it should work. Any idea why it does not?

Edited by ryanmetzler3
Link to comment
Share on other sites

  • Solution

Makes the form fail how? Are you getting an error?

 

You'd still be allowing the user to type in a username, but then forcibly replacing it - that's a little confusing for the user. It's also not fool-proof, as the user can still set the data before it reaches your server.

 

If you want to force their username, you should do it when processing the AJAX request.

 

You can set the input field attribute for "disabled", which will not let the user edit the field. This way they know what the value the name will be. You should still force the username when processing the AJAX request, though, as it's still possible for it to be modified by the user.

Link to comment
Share on other sites

I actually just got it working. It turns out I had the submit button and the login button with the same id and it was messing everything up. The idea was that if they were logged in, the name field would not even show up, therefore they would not be able to type anything in it. It might just welcome them by saying echo "welcome $username". Or if they were a guest it would show up. 

 

Thanks for the suggestions! also I will look at doing this on the ajax side since you recommended that. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.