简体   繁体   中英

How to create an option in a select dropdown that has no value in PHP

Below is code I use to determine the options of a select dropdown.

The problem I'm having is that, when neither November or May has been set, I expect the value of the first option to read value="" . However, PHP ignores the quotes and does not include them in the HTML resulting in just value . The reason why I need the quotes, is because I am using the form attribute required to validate the form, and when the value is not "" , validation considers the select as filled in.

I have tried value=\\"\\" , value=\\"''\\" and value=\\"""\\" among many other variations, but nothing seems to be working.

Any help would be appreciated.

function month() {
    global $link; 

    if(isset($_GET['month'])){
        $month=$_GET{'month'};
        if($month=='May') {
            echo "  
                <option value=\"May\" selected=\"selected\">May</option>
                <option value=\"November\">November</option>
                ";
        } else if($month=='November'){
            echo "  
                <option value=\"May\">May</option>
                <option value=\"November\" selected=\"selected\">November</option>
                ";
        } 
    } else {
        echo "
            <option value=\"\" disabled selected hidden>Month</option>
            <option value=\"May\">May</option>
            <option value=\"November\">November</option>
            ";
    }
}

Try using single quote (' ') for String in echo and double quote (" ") for value attribute in select tag:

echo '<option value="May" selected="selected">May</option>
      <option value="November">November</option>
';

That may help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM