Getting the selected option in $_POST

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
rowanbradley1
New php-forum User
New php-forum User
Posts: 7
Joined: Sat Feb 25, 2017 10:30 am

Mon Feb 27, 2017 5:11 am

I have a web page containing a form containing a select with name='section' and with several options with values like 1, 2, 3 etc. One of the options is marked "selected" when the form is displayed. When the form is submitted, I need to know whether any of the options has been selected by the user, or whether the original selection is still selected. Actually I don't care whether the user has selected anything or not, I just need to know what option is selected in the form when it is submitted. I find that often the variable $_POST['section'] is not set. Why does this happen? Does the browser not return the value of the select when the user has not selected anything (i.e. when the original selection is still in place)? If so this means that if the value is not set, I have to redo a complicated database lookup to find out what it was set to before in order to redisplay the page. It would be much better (for me) if it _always_ returned the value of the select, even if the user hasn't changed it. Is there a way to do this?

Thanks - Rowan
chorn
php-forum GURU
php-forum GURU
Posts: 654
Joined: Fri Apr 01, 2016 2:18 am

Mon Feb 27, 2017 5:22 am

you can never ever trust user input. (never!)
NigelRen
php-forum GURU
php-forum GURU
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Mon Feb 27, 2017 6:19 am

You can always store the original selected value in session data if needed, stop having to look it up again.
As for not trusting user data - that is an important thing to remember! We had penetration testing of our site and the 'user' had a browser extension which allowed them to change the values on the form - even 'read only' values. It's so easy to rely on the idea that what the user shouldn't be able to change stays the same - but also a big mistake when it comes to trusting it.
Post Reply