PDA

View Full Version : Radio button validation


prashanth
09-01-2013, 07:40 AM
Hi,

I want to validate the radio button from the form.I have validated other fields. Please give me an solution for validating the radio button such that radio button should not be empty.

Please find the code below.

Body Part:

<form name= "vehicle" id= "info" method= "post" action= "" >
<table width= "500" border= "0" align="center" cellpadding= "5" cellspacing= "1" class="table">
<tr>
<td width= "120" align="right" >Vehicle Number : </td>
<td width= "238" ><input name= "number" type= "text" value="<?php if(isset($number)){echo $number;} ?>" <?php if(isset($code) && $code == 1){echo "class=error" ;} ?> ></td>
</tr>
<tr>
<td align="right" width="120">Vehicle Name : </td>
<td><input name= "name" type= "text" id= "name" value="<?php if(isset($name)){echo $name; }?>"<?php if(isset($code) && $code == 2){echo "class=error" ;}?> ></td>
</tr>
<tr>
<td align="right" width="120">Vehicle User : </td>
<td><input name= "user" type= "text" id= "user" value="<?php if(isset($user)){echo $user;} ?>"<?php if(isset($code) && $code == 3){echo "class=error" ;}?> ></td>
</tr>
<tr><td align="right">Fuel : </td>
<td><input id="petrol" type="radio" name="fuel" value="<?php if(isset($fuel)){echo $fuel;} ?>"<?php if(isset($code) && $code == 4){echo "class=error" ;}?> >
<label for="petrol">Petrol</label>
<input id="diesel" type="radio" name="fuel" value="<?php if(isset($fuel)){echo $fuel;} ?>"<?php if(isset($code) && $code == 4){echo "class=error" ;}?> >
<label for="diesel">Diesel</label></td>
</tr>
<td>&nbsp;</td>
<td><input type= "submit" name= "Submit" value= "Submit" /></td>
</tr>
</table>
<table align="center">
<?php if (isset($error)) { ?>
<tr>
<td colspan="2" align="center" ><?php echo "<p class='message'>" .$error. "</p>" ; ?></td>
</tr>
<?php } ?>
</table>
</form>


Validation Code :

if(isset($_POST['Submit']))
{

$number=trim($_POST["number"]);
$name=trim($_POST["name"]);
$user=trim($_POST["user"]);
$fuel=trim($_POST["fuel"]);

//check if Vechile Number field is empty
if($number == "" )
{
$error= "error : You did not enter a Vechile Number.";
$code= "1" ;
}

//Check for Vehicle
elseif($name == "" )
{
$error= "error : You did not enter a Vechile Name.";
$code= "2";
}

//check for username
elseif($user == "" )
{
$error= "error : You did not enter a name of the user.";
$code= "3" ;
}

//Check for radio button
elseif($fuel == "")
{
$error= "error : You must select one option";
$code= "4" ;
}

else
{
//Database code

}
}
?>

Please give a solution for radio button validation part.

edbr
09-01-2013, 08:21 AM
your radio buttons have no value to subhmit.
try
<input id="petrol" type="radio" name="fuel[]" value="1<?php if(isset($fuel)){echo $fuel;} ?>"<?php if(isset($code) && $code == 4){echo "class=error" ;}?> >



<label for="petrol">Petrol</label>
<input id="diesel" type="radio" name="fuel[]" value="2<?php if(isset($fuel)){echo $fuel;} ?>"<?php if(isset($code) && $code == 4){echo "class=error" ;}?> >