Header Ads

Drop Down List Validation

<?php
$error=-1;
$errorcolor="";
$errorevent="";
$color = "";
$event = "";


function isSelected($val,$sval)
{
   if(is_array($sval))
   {
     foreach($sval as $lval)
 {
if($lval==$val)
{
echo 'selected="selected"';
break;
}
 }
   }
   else
   {
if($val==$sval)
{
echo 'selected="selected"';
}
   }

}
if(array_key_exists("btnSave",$_POST) and $_POST['btnSave']=='Save' )
{
$error=0;
//Process Form


$color = $_POST['ddlColor'];
$event = $_POST['ddlEvent'];

if($color==-1)
{
$error=1;
$errorcolor="* Select Color!!!";
}
if($event==-1)
{
$error=1;
$errorevent="* Select Event!!!";
}

if($error==0)
{
//Display Results
echo $color;
echo $event;







}

}
if($error <>0)
{
?>
<form id="form1" name="form1" method="post" action="">
  <table width="400" border="0" align="center" cellpadding="3" cellspacing="3">
    <tr>
      <td>Select a colour to your team:
        <label>
        <select name="ddlColor" id="ddlColor">
          <option value="-1">select</option>
          <option value="red" <?php isSelected('red',$color) ?>>red</option>
          <option value="blue" <?php isSelected('blue',$color) ?>>blue</option>
        </select>
        <?php echo $errorcolor; ?></label></td>
    </tr>
    <tr>
      <td>How many events have you:
        <label>
        <select name="ddlEvent" id="ddlEvent">
          <option value="-1">select</option>
          <option value="1" <?php isSelected('1',$event) ?>>1</option>
          <option value="2" <?php isSelected('2',$event) ?>>2</option>
        </select>
        <?php echo $errorevent; ?></label></td>
    </tr>
    <tr>
      <td><label>
        <input type="submit" name="btnSave" id="btnSave" value="Save" />
      </label></td>
    </tr>
  </table>
</form>
<?php

}
?>

No comments

Thank you very much for your ideas!