I have a form that has a option, in which the user is required to select a State. How can I validate this, server-side, with pure PHP? If the "Select State" option is selected, then server will return "not valid".
Thank you!
My HTML code (summary):
<select id="stateSel" name="state" onclick="stateValidation()">
<option value="0">Select State</option> /*will return as invalid option*/
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
/*[etc, options for each state]*/
<option value="WY">Wyoming</option>
</select>
You can do it like this:
here the code not only checks if there is a post field
state
but also it is comparing with possible values (in this case US states).State's default value is always 0 at load time if user didn't change it then you need to validate like below code
BELOW CODE IS FOR FORM SUBMIT
OR
BELOW CODE IS FOR JS FUNCTION
Value of only selected option is posted to server side when you submit a form.
If you are submitting form with
POST
method.