Jump to content

Validation of options


mikebyrne

Recommended Posts

Im trying to validate a dropdown list whereby if the user doesnt select an option an error will appear

 

My code is:

 

if($_POST["action"] == "Add"){

        $valid=1;
	if ($_POST['Type']=="------") {
			echo 'Please enter the product type<br>';
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_type = "Please enter the product type<br>";
									}	

<select name="Type" class="000">
<option value="">------</option>
<option value="Game" >GAME</option>

<option value="DVD" >DVD</option>
<option value="CD" >CD</option>
    		</select>
            <td width="269"><font color="#FF0000" style="font-size: 8pt"><?php echo $error_type; ?></font></td>

 

At present if I leave it at ----- I dont get a report

 

Link to comment
Share on other sites

i'm not sure that it will work, but you can try this:

 

 

<select name="Type" class="000">

<option value="------">------</option>

<option value="Game" >GAME</option>

 

<option value="DVD" >DVD</option>

<option value="CD" >CD</option>

    </select>

Link to comment
Share on other sites

The above poster's code will work.

 

In this example when using the drop down box:

<select name="Type" class="000">

<option value="">------</option> returns $_POST['Type'] = ""

<option value="Game" >GAME</option> returns $_POST['Type'] = "Game"

<option value="DVD" >DVD</option> returns $_POST['Type'] = "DVD"

<option value="CD" >CD</option> returns $_POST['Type'] = "CD"

    </select>

So you see, it assigns it to the value of the option, not the text inbetween the tags.

 

Changing the value to "------" isn't such a good idea, as it could cause problems later on,

but you can change the whole 'if' argument to:

if ($_POST['Type'] == "" || !isset($_POST['Type']))

or just one of the two there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.