I have a form for a ‘user profile’ with the following fields:
<form>
First Name: <input id="first_name" />
Last Name: <input id="last_name" />
Phone Number: <input id="phone_number" />
City: <input id="city" />
</form>
However, what if I don’t want “phone number” and “city” to be visible to a select set of users?
How can I dynamically hide these fields, depending on, say for example, which user is logged in? I am currently using PHP and MySQL. Would it be best to add the “field id’s” to the database, and then query the database record to see which fields should be hidden and which fields should be visible? Or is there a better way to do this?
My apologies if this is a bit ambiguous. If it doesn’t make sense, I can try to clarify.
Thanks very much.
EDIT: I’d like to clarify what I am trying to achieve. I am trying to dynamically show the fields based on which user logs in. Also, when I said “hidden”, I actually should have said “removed.” See my example below
E.g.,
If JoeUser logs in, he would see:
<form>
First Name: <input id="first_name" />
Last Name: <input id="last_name" />
Phone Number: <input id="phone_number" />
City: <input id="city" />
</form>
But, if JaneUser logs in, she would see (based on preferences set in the database):
<form>
First Name: <input id="first_name" />
Last Name: <input id="last_name" />
</form>
My question is: What is the best way to achieve this? I’m assuming these preferences need to be database-driven. Any input would be great.
Depending on how you’ve built the site there are quite a few ways to do this but since you’re new to PHP I’ll assume that it’s just an inline procedural script. In which case you need to check whether or not the user is set in the session and then show or hide depending on the output.
Something similar to the following
The if statment checks if a session ID is set (this will likely change depending on how you’re handling user sessions etc) and then shows the form.
Edit:
Just noticed that you’ve edited your original question. To do this you could so something similar to the following, this assumes once again that you’re using sessions and that you have the users user type set in a session (not great, but as a beginner it’s quick).