Page 1 of 1

How to retain the selected radio button as checked?

Posted: Wed Sep 03, 2008 2:11 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 11:10 am
by califdon
First you have to read your $_POST variables to determine which radio button has been clicked. Then, in the PHP code that generates the HTML for your form, you have to add the word "checked" inside the <input ... /> tag of the one that was checked before. Remember, only one button can be checked in a group having the same name.

Re: How to retain the selected radio button as checked?

Posted: Wed Sep 03, 2008 12:28 pm
by starram
Put a if condition something like this.
<input type="radio" value="1" id="radiotab" <? if($_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: Thu Sep 04, 2008 8:09 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 1:28 pm
by califdon
treesa wrote: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"?>/>

Code: Select all

 <input type="radio" value="1"  id="radiotab" <?php  if($_POST['radiotab']=="1") echo "checked='checked'"; ?>/>
Don't use the "short form" opening php tag, always use <?php.

And you need to echo (or print) whatever you want to be sent to the browser by php.

Re: How to retain the selected radio button as checked?

Posted: Thu Sep 04, 2008 1:41 pm
by starram
just write echo "checked='checked'";
As I suggested in the example.
This will be placed in the html tags and behave like that. If u are still confused give ur codes I will do that for u.

Re: How to retain the selected radio button as checked?

Posted: Fri Sep 05, 2008 12:06 am
by treesa
<input type="radio" name="radio1" value="str1"
<?php if($_POST['radio1']=="str1") echo "checked='checked'"?>/>Action 1<br/>

<input type="radio" name="radio1" value="str2"
<?php if($_POST['radio1']=="str2") echo "checked='checked'"?>/>Action 2<br/>

<input type="radio" name="radio1" value="str3"
<?php if($_POST['radio1']=="str3") echo "checked='checked'"?>/>Action 3<br/>


But the first time I run this code, I get the following error:
PHP Notice: Undefined index: radio1 on line 35 PHP Notice: Undefined index: radio1 in on line 38 PHP Notice: Undefined index: radio1 on line 41


later on,the code works fine.

Re: How to retain the selected radio button as checked?

Posted: Fri Sep 05, 2008 12:11 am
by starram
You can put another condition with this code.
Just check if isset($_POST[radio1])

For Example:

<input type="radio" name="radio1" value="str1"
<?php if( isset($_POST['radio1']) && $_POST['radio1']=="str1" ) echo "checked='checked'"; ?>/>Action 1<br/>

Re: How to retain the selected radio button as checked?

Posted: Fri Sep 05, 2008 7:19 am
by treesa
thanks..this worked fine!!

Re: How to retain the selected radio button as checked?

Posted: Fri Sep 05, 2008 9:29 am
by Bill H
The attribute started off being quoted correctly but somewhere got added to.
It doesn't really need to be "checked='checked'", it merely needs to be "checked" with a space in front of it.

Code: Select all

<?php if( isset($_POST['radio1']) && $_POST['radio1']=="str1" ) echo " checked"; ?>/>Action 1<br/>

Re: How to retain the selected radio button as checked?

Posted: Fri Sep 05, 2008 9:31 am
by jayshields
Bill H wrote:It doesn't really need to be "checked='checked'", it merely needs to be "checked" with a space in front of it.
It's always good to code for XHTML though, unless otherwise stated...

Re: How to retain the selected radio button as checked?

Posted: Mon Sep 08, 2008 2:02 am
by treesa
echo "checked" works without the space.