Default option value in a form | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Default option value in a form

I have a form with some options values, after the user choose one, I want the next time he visits the site, his last choose be pre selected, what's the best way to do it? I have the last value in a PHP cookie but I don't know how to use it in the html code :/

18th Oct 2021, 4:09 PM
José Luis Palacio
José Luis Palacio - avatar
4 Answers
+ 1
Thanks for your time Ipang, I'll check ✅
18th Oct 2021, 6:52 PM
José Luis Palacio
José Luis Palacio - avatar
0
What element are you using? I think you will need to also populate the options using PHP. During population of options to choose, you check whether one of the options matches the last value in cookie. If match found, you set that option selected. How to make an option get selected will depend on which element in use.
18th Oct 2021, 4:17 PM
Ipang
18th Oct 2021, 4:34 PM
José Luis Palacio
José Luis Palacio - avatar
0
Here's an example that runs in Code Playground. I had to hardcode the $last_option here because Code Playground doesn't allow form submission, and I'm not sure whether cookies were allowed either. https://code.sololearn.com/wE0DtrF3ZDKV/?ref=app
18th Oct 2021, 5:16 PM
Ipang