Forum Moderators: open

Message Too Old, No Replies

how can I give a set of radio buttons a default value that's hidden?

         

partha

5:15 am on Jan 11, 2005 (gmt 0)

10+ Year Member



I tried having a type="hidden" input with the same name as the radio buttons, but that just made the form always send my hidden default value, even if I pick a radio button.

moltar

5:18 am on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rename one of them

partha

4:44 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



what? but I need them all (the radio buttons and the hiddent default value) to pass their value to another page. how would renaming one of them work?

I'm sorry, I'm not getting it. could you explain more?

rocknbil

6:46 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The form object names must be unique (with the exception of radio buttons.) When you send a form, it sends it in key/value PAIRS:

<input type="hidden" name="my_item" value="123">
<input type="radio" name="your_item" value="456">
<input type="radio" name="your_item" value="789">

my_item=123
your_item=456 OR your_item=789 based on selection

If you name them the same, depending on which variable is parsed first, you will have one value overwriting the other:

<input type="hidden" name="your_item" value="123">
<input type="radio" name="your_item" value="456">
<input type="radio" name="your_item" value="789">

your_item=123

The exception is using some read/parse routines that don't do this, but you still get

your_item=123
your_item.1=456 OR your_item.1=789 based on selection

That's what moltar meant. :-)

partha

12:18 am on Jan 12, 2005 (gmt 0)

10+ Year Member



ok, but is there a way to make a variable with the same name have a default value that's submitted if no radio button is selected?

rocknbil

3:03 am on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You leave one of the radio buttons checked.

<input type="radio" name="my_button" value="value 1" checked>

No good? One of those people who thinks a radio button should not have a default checked state? :-)

On the server side,

if (! $qs{'my_button'}) { $qs{'my_button'} = 'default value'; }

(That's in perl, use the language of your choice.)

MatthewHSE

4:25 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just ran into this problem myself but finally found the answer. Partha, your initial solution is the correct one, only let me guess, you had the hidden field code after the code for the radio buttons, right? If so that's the problem - the hidden field needs to come first, then the radio buttons. Then if a radio button isn't selected, the default value will be used. If a radio button is selected, it's value will overwrite the default value.