0

I am trying to add a default value to the options in the select tag.

<form>
    <select>
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
</form>

I was wondering if you could set the default value as the value that the user chose. For example, if the user chose 2 and submitted the form, how can I set the default value to 2?

  • You would have to set a cookie storing their selection and then auto-select that option when they return. – APAD1 Jul 30 '14 at 03:04
  • 1
    refer [http://www.w3schools.com/](http://www.w3schools.com/tags/att_option_value.asp) – Darshan Patel Jul 30 '14 at 03:05
  • The value selected by the user will remain selected, so what is the point? If you mean that the value should be the default if the user later visits the page, please clarify it in the question. (Then you would need to use cookies or `localStorage`.) – Jukka K. Korpela Jul 30 '14 at 04:07
  • possible duplicate of [How to set default value for HTML select element?](http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element) – mattt Jul 30 '14 at 05:38

1 Answers1

0

Add the attribute "checked" to the option you want as the default. Something along the lines of...

<option>1</option> <option <?php echo $_POST["select_name"] == 2 ? "checked": ""; ?> >2</option><option>3</option>

Bollis
  • 385
  • 1
  • 11