Link to home
Start Free TrialLog in
Avatar of al4629740
al4629740Flag for United States of America

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>

Open in new window


Avatar of Scott Fell
Scott Fell
Flag of United States of America image

What you have should work in that the first option is "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. 
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.
Avatar of al4629740

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?
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.

<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> 

Open in new window


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.
Here is what I have on a text field but not sure a dropdown can do the same things

<div class="form group col-md-4">
            <label for="lblemail">Email</label>
            <input type="text" name="Email" class="form-control" placeholder="Enter your answer" maxlength = "49">
            </div>

Open in new window


User generated image
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>  
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.
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.
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.
In the example you updated, it still defaults to the first selection and the label is gone.

User generated image
ASKER CERTIFIED SOLUTION
Avatar of al4629740
al4629740
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all for your help!

Reference: ​Link​​​
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>

Open in new window