How to retain the selected radio button as checked?
Moderator: General Moderators
How to retain the selected radio button as checked?
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
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?
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?
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"; ?> />
<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?
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"?>/>
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?
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'"; ?>/>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?
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.
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?
<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.
<?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?
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/>
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?
thanks..this worked fine!!
- 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?
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.
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/>- 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?
It's always good to code for XHTML though, unless otherwise stated...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.
Re: How to retain the selected radio button as checked?
echo "checked" works without the space.