how to retain the values of radio buttons back from databese?

for example,

if user provided with 3 options (red, blue & green)

if a user selected green(the value is stored in database)
then next time he/she returning to the same page, previously selected option green radio button need to be selected not default button or else selected

values are got back by using query.

Thnx...

The first thing you would need to do is put the data into a session so it can be accessed later. So in your login form, when you gather the database info do something like:

$_SESSION['color'] = $row['color'];

Then on your form itself do something like:

<form name="form" action="myform.php" method="POST">
<input type="radio" name="red" value="Red" <php? if ($_SESSION['color'] == 'red') {
echo 'checked';
}?>> Red<br>
<input type="radio" name="blue" value="Blue" <php? if ($_SESSION['color'] == 'blue') {
echo 'checked';
}?>> Blue<br>
<input type="radio" name="green" value="Green" <php? if ($_SESSION['color'] == 'green') {
echo 'checked';
}?>> Green
</form>

Don't quote my syntax, because I am not at a PHP server to test, but hopefully this should get you headed in the right direction.

Also, if you don't want to use sessions, you can do it like:

<?PHP

$red = 'unchecked';
$blue = 'unchecked';
$green = 'unchecked';

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

$color = $_POST['color'];

if ($color = = 'red') {
$red = 'checked';
}
else if ($color = = 'blue') {
$blue = 'checked';
}
else if ($color = = 'green') {
$green = 'checked';
}
}

?>

Then you would just add:

<?php echo $red; ?>
<?php echo $blue; ?>
<?php echo $green; ?>

in your form tags. Hope this helps.

thnx for answer but i need something other...

prblm is not @ creating and checking radio button and posting the values to database... after that only...

DB contains value of colur which is selected, if user need edit same page his/her previous data should retained*(prblm here). how to display his/her old choice

thnx again...

So you are saying when the user goes back to the form it needs to show the correct value from the database that they chose previously? That is what it sounds like to me, and the code below is how you would do it.

thnx, i'l go by ur code & reply u again...

html
with clickable label

<input type="radio" name="color" id='red' value="Red" <?php if ($_SESSION['color'] == 'Red') {echo 'checked';}?>><label for='red'> Red</label><br>
<input type="radio" name="color" id='blue' value="Blue" <?php if ($_SESSION['color'] == 'Blue') {echo 'checked';}?>><label for='blue'> Blue</label><br>
<input type="radio" name="color" id='yellow' value="yellow" <?php if ($_SESSION['yellow'] == 'yellow') {echo 'checked';}?>><label for='yellow'> Yellow</label><br>

xhtml
with clickable label

<input type="radio" name="color" id='red' value="Red" checked="<?php if ($_SESSION['color'] == 'Red') {echo 'checked';}?>"  /><label for='red'> Red</label><br>

Case altered, 'Red' does not equal 'red'
radio button groups have the same name, that name should be the same name as the value being examined in the $session array, or the response will always be negative/nul/0/no

is it possible without creating session? (may be silly but i'm new to php web world)

it can, but sessions are actually easier

returning the value from a sql query the results can be directly assigned to a variable, instead of through the session array,
the session means the information is persistent until the session is closed,
much better if the information has to be retained, without posting it through post or get values between each file
It would probably be best, to spend the relatively short time needed to learn how to create use and destroy sessions
than the extra coding needed to pass the variables back through html gets or posts

i've used 2nd way(without session)...
i retrived value from DB & stored in a new variable...
i dont know where to echo that variable r else..

could u help?

i've used 2nd way(without session)...
i retrived value from DB & stored in a new variable...
i dont know where to echo that variable r else..

could u help?

Just replace the $_SESSION variable in the posts above, with your variabl <input bla bla <?php if($variable='red') { echo "checked"; }} ?> >



Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0


Mr Tekkno

Your code is right

But both the radio buttons r left unchecked if user enters into the page not the values from the database get displayed..

Here you go:

<form name="form" action="myform.php" method="POST">
<input type="radio" name="red" value="Red"
<?php echo $red;?>> Red<br>
<input type="radio" name="blue" value="Blue" <?php echo $blue;?>> Blue<br>
<input type="radio" name="green" value="Green" <?php echo $green;?>> Green
</form>

Then the code from below goes above your HTML, but I just noticed the spaces between the = there are no spaces, so it is ==. Another thing I wanted to mention, there has to be a way to determine what user it is. So you would really need a session to make it work properly. You could use client side cookies, but if they are deleted by the user the form will not be able to determine who they are when they come back.

<html >
<head>
</head>
<?PHP			  
$am = 'unchecked';
$pm = 'unchecked';


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

	$selected_radio = $_POST['radiogroup1'];
	
		if ($selected_radio == 'am') {
			$am = 'checked';

		}
		else if ($selected_radio == 'pm') {
			$am = 'checked';
		}
}

?>
<body>
<form id="form1" name="form1" method="post" action="prof_pers_update.php">
 <label>
                    <INPUT TYPE = 'Radio' Name ='radiogroup1'  value= 'am' <?PHP echo $am; ?>>
                    <strong>am</strong></label>
                  <label>
                    <INPUT TYPE = 'Radio' Name ='radiogroup1'  value= 'pm' <?PHP echo $pm; ?>>
                    <strong>pm</strong></label>
                  <br /><input type="submit" name="update1" id="update1" value="Update" />
</body>
</html>

I think the code submitted by u and me are logically same....

the above code displays only unchecked radiobuttons in the page. Not the value set in the database.

good working... tnx to almostbob... very helpful...

code working using session & also using variable(without session)...

thnx to those who all responded...

good working... tnx to almostbob... very helpful...

code working using session & also using variable(without session)...

thnx to those who all responded...

Thnx to those who helped... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.