al4629740
asked on
Placeholder needed on a dropdown in PHP
In PHP I am trying to make a placeholder that says select rather than a dropdown selection. How do I modify the code below to accomplish that?
<div class="form group col-md-4">
<label for="lblethnicity">Please provide your ethnicity</label>
<select name="Ethnicity" class="form-control">
<option value="Select">Select</option>}
<option value="Hispanic">Hispanic</option>
<option value="Latino">Latino</option>
<option value="Spanish Origin">Spanish Origin</option>
<option value="Non-Hispanic">Non-Hispanic</option>
</select>
</div>
Hi,
I would recommend to use Select2 this is very flexible and allow to search in dropdown.
https://select2.org/getting-started/basic-usage
So you can use any placeholder you want, the user can select or type something into the box.
I would recommend to use Select2 this is very flexible and allow to search in dropdown.
https://select2.org/getting-started/basic-usage
So you can use any placeholder you want, the user can select or type something into the box.
ASKER
Scott, thats a good idea, but I was hoping to still put a placeholder if possible.
Is there any way to put a placeholder there in html without having to add on another feature like Select2?
Is there any way to put a placeholder there in html without having to add on another feature like Select2?
I am not 100% sure what you mean by a placeholder.
The top item will always be the default unless you hard code in "selected" like below will default to Latino.
The top item will always be the default unless you hard code in "selected" like below will default to Latino.
<select name="Ethnicity" class="form-control">
<option value="Hispanic">Hispanic</option>
<option value="Latino" selected>Latino</option>
<option value="Spanish Origin">Spanish Origin</option>
<option value="Non-Hispanic">Non-Hispanic</option>
</select>
ASKER
I hope I know what I'm talking about but I believe a placeholder just puts faint text there until you make a real selection.
ASKER
Try optgroup:
<select name="Ethnicity" class="form-control">
<optgroup label="Select one">
<option value="Hispanic">Hispanic</option>
<option value="Latino">Latino</option>
<option value="Spanish Origin">Spanish Origin</option>
<option value="Non-Hispanic">Non-Hispanic</option>
</optgroup>
</select>
<select name="Ethnicity" class="form-control">
<optgroup label="Select one">
<option value="Hispanic">Hispanic</option>
<option value="Latino">Latino</option>
<option value="Spanish Origin">Spanish Origin</option>
<option value="Non-Hispanic">Non-Hispanic</option>
</optgroup>
</select>
ASKER
I tried optgroup and it returns an access denied message. Why is that? Is it a feature that belongs to a vendor?
No, it's just standard HTML. If you got access denied, it has nothing to do with optgroup. Maybe you were developing this page / code within some kind of authenticated session and the session expired?
Just as a side note, HTML doesn't have a vendor. It's just a set of generic instructions that the browser turns into something visual.
The only thing optgroup does is group options together under an unselectable header. That header can be used to show a "please select one" type of placeholder.
Just as a side note, HTML doesn't have a vendor. It's just a set of generic instructions that the browser turns into something visual.
The only thing optgroup does is group options together under an unselectable header. That header can be used to show a "please select one" type of placeholder.
ASKER
Ok I got it working. I like it, but it still doesn't seem to be working as a placeholder.
Here is why I need the placeholder. When I check the fields for validation, it keeps picking up the "Select" as a value and sending it to the database.
I think I found my solution based on the discussion. I will have to validate the dropdown fields differently than the rest.
Here is why I need the placeholder. When I check the fields for validation, it keeps picking up the "Select" as a value and sending it to the database.
I think I found my solution based on the discussion. I will have to validate the dropdown fields differently than the rest.
Oops I realized I didn't remove the additional "Select" option in my example. The optgroup can replace that (so you can take out the placeholder option). Since the optgroup header isn't selectable, it shouldn't send a value at all if an option isn't actually selected.
Just updated my above example.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
interesting from your link
<select>
<option disabled hidden selected>Select One</option>
<option>Choose Me!</option>
<option>No Me!</option>
<option>Me Me Me!</option>
</select>
I would probably make the value with nothing rather than, "Select" that way you can better find this field if not filled out as you may change this later.