ryanmetzler3 Posted December 16, 2013 Share Posted December 16, 2013 (edited) 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 December 16, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/284801-hiding-html-form-if-user-is-logged-in/ Share on other sites More sharing options...
Solution scootstah Posted December 16, 2013 Solution Share Posted December 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284801-hiding-html-form-if-user-is-logged-in/#findComment-1462465 Share on other sites More sharing options...
ryanmetzler3 Posted December 16, 2013 Author Share Posted December 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284801-hiding-html-form-if-user-is-logged-in/#findComment-1462473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.