Skip to content
Advertisement

Keep a check whether a value if selected from dropdown list using PHP

I need to keep a check on whether a value is selected by the user from the dropdown box other than the default value which is at null position. If selected (any value other than default ) then I want to set a variable say $varSet = 1 and if not $varSet = 0, so depending on the $varSet value, I try to trigger the appropiate functions from my PHP script.

Is it possible to achieve this using only PHP and HTML without any Javascript(client side)? Or is there any other possibilty to get this logic going?

Advertisement

Answer

Let’s assume that you have form with method=”POST” and action=”script.php”. Form would have select tag (I assume that is what you call dropdown list):

<select name="dropdown">
  <option value="">Default value (key thing is leaving value="")</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

And script.php would read value of dropdown list and check if default value is checked:

if($_POST['dropdown'] != '') {
  //do something
} else {
  //user leaved default value for dropdown, tell him that:
  echo "Please select valid value from dropdown list";
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement