Tuesday, February 1, 2011

Radio buttons-PHP

Assalamualaikum and selamat sejahtera

Okey,for this we will learn about radio button.what is radio button?to know more on radio button..

So,first of all,make a file named RadioButton.php and here is the code :

<html>

<head>
<title>Lessons On Radio Buttons</title>
</head>

<body>
<Form name ="formOne" Method ="Post" ACTION ="RadioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male'>Male
<Input type = 'Radio' Name ='gender' value= 'female'>Female
<P>
<Input type = "Submit" Name = "SubmitOne" Value = "Select a Radio Button">
</FORM>
</body>

</html>

A little explanation on this HTML script.ok,why i type  "value= 'male'>Male" or "value= 'female'>Female".just concentrate at ">Male" this one is for display.if we change it to Maleloolo,then it will display like this:
understand?ok shall we go to PHP script.Here is the code:


<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';


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

$selected_radio = $_POST['gender'];
if ($selected_radio == 'male') 
{
$male_status = 'checked';
}
 

else if ($selected_radio == 'female') 
{
$female_status = 'checked';
}
 

}
?>
Make sure the PHP script at head tag section.

Noticethis one : $male_status = 'unchecked' or $female_status = 'unchecked'.Okey for this one,this line will unchecked the value of radio button.quite blurry ryte.oke here is the example:

before we click the submit button.The blue point is at 'Male'.

after.what happen is the blue point is gone!so to keep it display even we already click the submit button.

Please,modify your HTML form like this (the bold and green is new code):

<Form name ="form1" Method ="Post" ACTION ="RadioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male
<Input type = 'Radio' Name ='gender' value= 'female'<?PHP print $female_status; ?>>Female
<P>
<Input type = "Submit" Name = "SubmitOne" Value = "Select a Radio Button">
</FORM>

Here is the full code:

<html>
<head>
<title>Lessons On Radio Buttons</title>

<?PHP

$male_status = 'unchecked';
$female_status = 'unchecked';

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

$selected_radio = $_POST['gender'];

if ($selected_radio == 'male') {
$male_status = 'checked';
}
else if ($selected_radio == 'female') {
$female_status = 'checked';
}
}

?>

</head>
<body>

<Form name ="form1" Method ="Post" ACTION ="RadioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male
<Input type = 'Radio' Name ='gender' value= 'female'<?PHP print $female_status; ?>>Female
<P>
<Input type = "Submit" Name = "SubmitOne" Value = "Select a Radio Button">
</FORM>

</body>
</html>



N/NOTE:try it!

1 comment: