logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 09-01-2013, 07:40 AM   #1
prashanth
 
Join Date: Aug 2013
Posts: 3
Default Radio button validation

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.
prashanth is offline   Reply With Quote
Old 09-01-2013, 08:21 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

your radio buttons have no value to subhmit.
try
Code:
<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" ;}?> >
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:44 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com