Page 1 of 1
How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 2:05 am
by treesa
i am having 5 to 6 radio buttons in the first page of a php code.depending on the
radio button selected,i perform a particular function using if statements.
I have created a form and I call the same file itself again using form action=" ".
My main problem is that each time i select a radio button,when the result gets displayed,
another radio button for which ive given checked="checked" gets selected.
how do i ensure that each time the result is displayed,the radio button which i selected
to get the result,remained checked?
thanks for any help
Re: How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 3:13 am
by JAB Creations
Code: Select all
<input <?php if (isset($_POST['my_checkbox_01'])) {echo 'selected="selected" ';}?> name="my_checkbox_01" value="stuff" />
Re: How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 10:07 am
by treesa
Thanks for the help.But i still havent received the required answer.
Assume there are 3 radio buttons in the form.now suppose , first time, the first radio button is checked by default.
now this is the problem...if i am checking the 2nd radio button to perform a particular action,when the result is displayed(by calling the same form itself),the first radio button gets checked again.
i need to ensure that when the 2nd radio button is selected,when the result is displayed,the 2ND RADIO BUTTON itself remains checked.
Re: How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 10:18 am
by JAB Creations
Well you're mixing the terminology up which will negate the effectiveness of communicating with others...
Radio buttons = selected.
Check boxes = checked.
Another common mistake is that many people refer to (X)HTML elements as "tags" in example.
Also it sounds more like a clientside issue than a serverside issue. Have you tried the code?
What you need to do is clearly explain the exact action you are taking that creates the issue of the wrong radio button being selected. Are you pressing a key? Are you clicking on something?
It could still be a serverside issue...are you checking to make sure the name and the $_POST['my_var_here'] match?
Re: How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 1:00 pm
by starram
Put a if condition something like this.
Code: Select all
<input type="radio" value="1" id="radiotab" <? if(!isset($_POST['radiotab']) or $_POST['radiotab']=="1") echo "checked"; ?> />
<input type="radio" value="2" id="radiotab" <? if($_POST['radiotab']=="2") echo "checked"; ?> />
<input type="radio" value="3" id="radiotab" <? if($_POST['radiotab']=="3") echo "checked"; ?> />
Re: How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 8:19 pm
by JAB Creations
AH-HA!! I see
exactly what you're doing wrong here...
You've mistaken the
id and the
name attributes!
Also I think I have the radio/checked and checkbox/selected backwards!
Example code...
<label for="
radio1" style="border: #000 dotted 1px; display: block; float: left; width: 200px;">First Name</label><input id="
radio1" name="first_name" style="border: #000 solid 1px; display: block; float: left; width: 400px;" value="" />
That is an example of how I style
label elements with their associated
input elements for type = radio, text, and also for
select elements.
Have a look at the top of my website's sidebar...
http://www.jabcreations.com/blog/
What you want is the
name attribute!
The clientside to serverside delivery....
<input name="
first_name" value="" /> == $_POST['
first_name']
<input name="
last_name" value="" /> == $_POST['
last_name']
<input name="
company" value="" /> == $_POST['
company']
HTML
<input name="example_678" value="Hi mom!" />
PHP
echo $_POST['example_678']; // echos Hi mom!
So the
name attribute's value is what is sent as the
container with the
value that PHP will be able to handle.
Take a look at the code in this form, this is pretty easy once you have a working example...
Code: Select all
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Form Example</title>
<style type="text/css">
div.width_50 {float: left; width: 50%;}
form fieldset {border: #039 solid 2px; margin-bottom: 8px;}
form fieldset, form fieldset div {clear: both;}
form fieldset input.text {border: #000 solid 1px; height: 18px; margin: 1px; width: 30%;}
form fieldset input.text:hover, form fieldset input.text:focus {border: #039 solid 2px; height: 18px; margin: 0px;}
form fieldset label {border: #000 dotted 1px; display: block; float: left; height: 20px; margin: 1px; padding-left: 2px; width: 200px;}
form fieldset label:hover {border: #000 dashed 2px; cursor: pointer; float: left; margin: 0px;}
form fieldset legend {background-color: #039; color: #fff; padding: 0px 4px 0px 4px;}
</style>
</head>
<body>
<h1>Form Example</h1>
<form action="" method="post">
<div class="width_50">
<fieldset>
<legend>Basic Information</legend>
<div><label for="name_1">First Name</label><input class="text" id="name_1" name="name_first" type="text" value="<?php echo $_POST['name_first'];?>" /></div>
<div><label for="name_2">Last Name</label><input class="text" id="name_2" name="name_last" type="text" value="<?php echo $_POST['name_last'];?>" /></div>
<div><label for="location">Location</label><input class="text" id="location" name="location" type="text" value="<?php echo $_POST['location'];?>" /></div>
</fieldset>
</div><!-- /.width_50 -->
<div class="width_50">
<fieldset>
<legend>What is your favorite color?</legend>
<div><label for="red"><input id="red" name="color" <?php if ($_POST['color'] == "color_red") {echo ' checked="checked"';}?>type="radio" value="color_red" />Red</label></div>
<div><label for="green"><input id="green" name="color" <?php if ($_POST['color'] == "color_green") {echo ' checked="checked"';}?>type="radio" value="color_green" />Green</label></div>
<div><label for="blue"><input id="blue" name="color" <?php if ($_POST['color'] == "color_blue") {echo ' checked="checked"';}?>type="radio" value="color_blue" />Blue</label></div>
</fieldset>
</div><!-- /.width_50 -->
<fieldset>
<legend>Options</legend>
<input type="submit" value="Submit this Form" />
</fieldset>
</form>
</body>
</html>
Re: How to retain the selected radio button as checked?
Posted: Wed Sep 03, 2008 11:28 pm
by starram
You can use both name and id attributes. This is what I do in my coding to handle this situation.
Re: How to retain the selected radio button as checked?
Posted: Thu Sep 04, 2008 8:10 am
by treesa
thanks for the help
if i need to give the following,how do i do it?Since Checked ="checked" is html coding,
the rest is php coding,how do i implement it?
<input type="radio" value="1" id="radiotab" <? if($_POST['radiotab']=="1") checked="checked"?>/>
Re: How to retain the selected radio button as checked?
Posted: Thu Sep 04, 2008 8:25 am
by JAB Creations
In JavaScript you
alert. In PHP you
echo!
Code: Select all
//echo a string
echo 'hi mom!';
//echo a variable
$my_var = 'Hi mom!';
echo $my_var;
//echo a function
function hello() {$a_var = 'hi mom'; return $a_var;}
echo hello();
I have a couple of
important notes but here is the code first...
Code: Select all
<input type="radio" value="1" id="radiotab" <? if($_POST['radiotab']=="1") {echo 'checked="checked" ';?>/>
When you echo an attribute in PHP to be output in (X)HTML
make sure you have the proper spacing between each (X)HTML attribute!
Here are
three examples, thefirst two are bad where the checked attribute doesn't have a space between it and the type attribute. The second bad example does not have a space between it and the value attribute. The third is what the output
should look like as far as spacing is concerned...
Bad Example: XHTML output #1
<input type="checkbox"checked="checked" value="" />
Bad Example: XHTML output #2
<input type="checkbox" checked="checked"value="" />
GOOD Example: XHTML output
<input type="checkbox" checked="checked" value="" />
Re: How to retain the selected radio button as checked?
Posted: Thu Sep 04, 2008 1:25 pm
by starram
<input type="radio" value="1" id="radiotab" <? if($_POST['radiotab']=="1") echo "checked='checked'"; ?> />
This is the example. please let me know what else you want to know.