How can I use PHP to validate a drop down list

I want to validate a form using PHP. Below is the sample of my form:

<form>
    <select name="select_box">
        <option value="0">Please Select</option>
        <option value="1">OPT 1</option>
        <option value="2">OPT 2</option>
    </select>
    <input type="submit" value="Go!" />
</form>

Upvotes: 2

Views: 26494

Answers (3)

Vidyadhar
Vidyadhar

Reputation: 1118

<?php 
if(isset($_REQUEST['select_box']) && $_REQUEST['select_box'] == '0') { 
  echo 'Please select a country.'; 
} 
?>

Upvotes: 1

Andreas Wong
Andreas Wong

Reputation: 60516

In your php script where are you are submitting your form (in this case it's the same script that echoes out the form, since you aren't specifying action and method attribute to your <form>), you can get values of inputs by doing $_GET['name_of_input'], which in your case:

if(isset($_GET['select_box'])) { // do something with value of drop down

Upvotes: 1

egis
egis

Reputation: 1412

<?php
if ($_GET['select_box'] != '0') //form validation
{
 //your code here
}
?>

Upvotes: 0

Related Questions