Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to use radio buttons...

Status
Not open for further replies.

kpdvx

Programmer
Dec 1, 2001
87
US
What is the best way to use radio buttons? I know how to insert their value, but, I also want to make an edit form, where the current value of the radio button is selected, but you could also change it. Say if in the database, radio4 was selected; when you edited that form, the radio button with the name of radio4 would be "pushed in". What is the best way to implement this?
 
This isn't a PHP question -- it's an HTML question. But I'll answer.

If the value from the database is 2 and the possible values are 1, 2, and 3, the following will produce a set of buttons with 2 already selected:
<input type=radio name=foo value=1>
<input type=radio name=foo value=2 checked>
<input type=radio name=foo value=3>
Want the best answers? Ask the best questions: TANSTAAFL!
 
ahem, it is a PHP question. Here is a bad way to do it:

SELECT * FROM table WHERE blah=blah.

if ($one == 'checked') {
$checkedone = 'checked';
}
if ($two == 'checked') {
$checkedtwo = 'checked';
}
if ($three == 'checked') {
$checkedthree = 'checked';
}

<input type=radio name=foo value=1 <?=$onechecked?>>
<input type=radio name=foo value=2 <?=$twochecked?>>
<input type=radio name=foo value=3 <?=$threechecked?>>

Is there a simpler, cleaner way to do this?
 
Yes...

First step to clean it up is to make some arrays in php, and loop over them rather than declaring each one explicitly.

so I'm not sure where $one,$two,$three came from... but instead say. (sorry if my for loop syntax is off, too many different languages today)

for ($i=0, $i < count($checked), $i++) {
echo &quot;<input type=radio name=foo value=$i &quot;;
if $checked = true {
echo &quot;checked>\n&quot;;
} else {
echo &quot;>\n&quot;;
}

-Rob
 
Hmmm, tek-tips butchered that...

that's supposed to be if

$checked[ $i ] == true

and the rest is right.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top