Forum Moderators: coopster

Message Too Old, No Replies

Radio buttons checked

         

Modern Merlin

6:15 am on Dec 6, 2007 (gmt 0)

10+ Year Member



In the form I am writing I have a group of radio buttons that that chooses either DVD, CD or VHS. The variable passes between the form and even updates the database. All of that is working just fine. If I ask the Variable that passes the information to echo it to the screen it displays correctly.

The problem I am having is when I go to pull the form up again with the already saved information in it to edit something else, this field will not mark either DVD, CD, or VHS with that is saved in the database already... Here is the code:

For the form


<input name="medium" type="radio" value="1" id="dvd"<?php echo dvd;?>>
<input name="medium" type="radio" value="2" id="cd"<?php echo cd;?>>
<input name="medium" type="radio" value="3" id="vhs"<?php echo vhs;?>>

For the backend and setting the variable to be checked


// if field is not empty
if (isset($_POST['medium'])) {
$selected_radiobtn = $_POST['medium'];


// set the value
if ($selected_radiobtn == '1') {
$dvd = 'checked';
}elseif($selected_radiobtn == '2') {
$cd = 'checked';
}elseif($selected_radiobtn == '3') {
$vhs = 'checked';
// if it is empy
} else {
// set the value to NULL
$medium = '';
}

Did a search on here and found this but Im not sure what I am doing wrong. [webmasterworld.com ]

Any help would be great! Been pulling my hair out all day on this one!

Modern Merlin

[edited by: Modern_Merlin at 6:31 am (utc) on Dec. 6, 2007]

cameraman

6:33 am on Dec 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For one thing you need your dollar signs:
<?php echo $dvd;?>

but you'll also need a space in there or you'll end up with:
<input name="medium" type="radio" value="1" id="dvd"checked>

It's also a good idea to set the variables to blank beforehand so you don't generate any errors (which you may not see depending on the setting of your error level):
$dvd = '';
$cd = '';
$vhs = '';
if (isset($_POST['medium'])) {
.
.

Modern Merlin

9:13 am on Dec 6, 2007 (gmt 0)

10+ Year Member



WOW I am so sorry I wasted your time on that! But thank you so much! THAT WORKED! Guess it helps if you dont do updates for 10 hours straight and then come at coding with a worn out brain for another 10 hours! :)

Thanks again!

Modern Merlin