Header Ads

Radio Button Validation

<?php
$error = -1;

$errorgender = "";
$errorstatus = "";
$gender = "";
$status = "";
function isChecked1($val,$sval)

{
if($val==$sval)
{
echo 'checked="checked"';
}
}

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

if(array_key_exists('radiog',$_POST))
{
$gender=$_POST['radiog'];
}
else
{
//$gender="";
$error=1;
$errorgender="* required field";
}
if(array_key_exists('radios',$_POST))
{
$status=$_POST['radios'];
}
else
{
//$status="";
$error=1;
$errorstatus="* required field";
}


if($error==0)
{
//Display Results

echo "Gender : ".$gender."<br>";
echo "Status : ".$status."<br>";

}

}
if($error <>0)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="400" border="0" align="center">
    <tr>
      <td><strong>Gender <?php echo $errorgender; ?> </strong></td>
    </tr>
    <tr>
      <td><label>
        <input type="radio" name="radiog" id="rdgender" value="Male" <?php isChecked1("Male",$gender) ?>/>
      </label>
      Male</td>
    </tr>
    <tr>
      <td><label>
        <input type="radio" name="radiog" id="rdgender2" value="Female" <?php isChecked1("Female",$gender) ?>/>
      Female</label></td>
    </tr>
    <tr>
      <td><strong>Status <?php echo $errorstatus; ?> </strong></td>
    </tr>
    <tr>
      <td><label>
        <input type="radio" name="radios" id="rdstatus" value="Single" <?php isChecked1("Single",$status) ?>/>
      Single</label></td>
    </tr>
    <tr>
      <td><label>
        <input type="radio" name="radios" id="rdstatus2" value="Married" <?php isChecked1("Married",$status) ?>/>
      Married</label></td>
    </tr>
    <tr>
      <td><label>
        <input type="submit" name="btnSave" id="btnSave" value="Save" />
      </label></td>
    </tr>
  </table>
</form>
</body>
</html>
<?php

}
?>

No comments

Thank you very much for your ideas!