How to retain the selected radio button as checked?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

How to retain the selected radio button as checked?

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

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

Post 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"; ?> />
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

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

Post 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"?>/>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

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

Post 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.
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

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

Post 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.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

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

Post 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/>
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

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

Post by treesa »

thanks..this worked fine!!
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

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

Post 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/>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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...
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

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

Post by treesa »

echo "checked" works without the space.
Post Reply