I added some radio buttons to a PHP form in osCommerce. I plan on adding a bunch more but in the meantime I have four right now. I’m wondering if I need to set all the radio buttons as true/false or just the single instance I want as true.
Here’s the code I have:
if (!isset($pInfo->tab_id)) $pInfo->tab_id = '1';
switch ($pInfo->tab_id) {
case '0': $none_tab_id = true; $shirt_tab_id = false;
$coverall_tab_id = false; $glove_tab_id = false;
break;
case '1': $coverall_tab_id = true; $shirt_tab_id = false;
$none_tab_id = false; $glove_tab_id = false;
break;
case '2': $coverall_tab_id = false; $shirt_tab_id = true;
$none_tab_id = false; $glove_tab_id = false;
break;
case '3': $glove_tab_id = true; $coverall_tab_id = false;
$shirt_tab_id = false; $none_tab_id = false;
break;
default: $coverall_tab_id = false; $shirt_tab_id = true;
$none_tab_id = false;
}
.
<?php
echo ' None' . tep_draw_radio_field('tab_id', '0', $none_tab_id) .
' coveralls' . tep_draw_radio_field('tab_id', '1', $coverall_tab_id) .
' Shirts' .tep_draw_radio_field('tab_id', '2', $shirt_tab_id) .
' Gloves' . tep_draw_radio_field('tab_id', '3', $glove_tab_id);
?>
osCommerce defines the tep_draw_radio_field
function as this:
tep_draw_radio_field($name, $value = '', $checked = false, $compare = '')
Is it necessary to set all the variables to false
in each case or if can I just set the one I want to true?
4
Answers
Why not just do something like this?
In the
tep_draw_radio_field()
function the only required parameter is the name variable, of which the radio button will use.The rest of the arguments are optional and have defaults already set for them.
You don’t need to code all of the radio buttons to either true or false. The default argument is
false
and any that aren’t explicitly set when you call the function will be set as such.Just set the one you need to be true as
true
. When you do, make doubly sure that it’s also the third argument.You could tighten up the switch statement:
You don’t need to code all of the radio buttons to either true or false. The default argument is false and any that aren’t explicitly set when you call the function will be set as such.
Just set the one you need to be true as true. When you do, make doubly sure that it’s also the third argument.