-1

I HAVE searched on here but, despite finding LOTS of similar and most likely MUCH more complicated situations, couldn't find an answer to what I am wanting to do.

Basically, on my site's search form I want to present 2 radio button options:

<label><input type="radio" name="question_9[]" value="Blue" checked="checked" />Blue</label>    
<label><input type="radio" name="question_9[]" value="Green" />Green</label>

BUT what I want to be submitted with the search form is:

<input type="hidden" name="question_9[]" value="Blue"  checked="checked"/>
<input type="hidden" name="question_9[]" value="Blue/Green"  checked="checked"/>

OR

<input type="hidden" name="question_9[]" value="Green"  checked="checked"/>
<input type="hidden" name="question_9[]" value="Blue/Green"  checked="checked"/>

depending on which radio button is checked when the form is submitted.

The software I am using recently migrated from scriptaculous to JQuery, which I am completely new to. I am hoping there is an easy way to pass 2 values for the question based on which radio button is selected, since as far as I know I can only select ONE value with radio buttons unless I do a workaround like this. Since the radio buttons themselves won't/shouldn't be submitting values with the form, they can be renamed or whatever needs be done to make it work.

I really appreciate any tips and again, I looked and didn't find another question asking to do the same as this (kind of surprised me) but HOPE that I am not being redundant with a question like this.

Denny
  • 15
  • 6

1 Answers1

0

You won't be able to submit two values with the same name - whether you use radio buttons, checkboxes, a hidden text field, or whatever. You need to think about what the PHP/ASP script processing your data will see - e.g. for PHP, what value should $_POST['question_9'] (or $_GET['question_9']) have? That value needs to be passed from a single field.

The good news is that you don't need any fancy javascript tricks to pass the right info to your script. If you want your PHP variable to be "Blue Blue/Green" you can just change the 'value' element of your radio button to "Blue Blue/Green".

You'll need to parse your values out on the PHP side - e.g.

$colorArray = explode(' ',$_POST['question_9'])

The PHP explode() will break a string into an array using a delimiter, in this example using a space.

EDIT:

In response to the clarification, the solution is simpler. The radio button will get you whether Blue or Green is submitted - if you want to also submit Blue/Green with it as part of the "question_9" array regardless of whether Blue or Green is checked, you should be able to just add a single hidden field like you said (removing 'checked' attribute since it only applies to checkboxes and radio buttons):

<input type="hidden" name="question_9[]" value="Blue/Green"/>
JSP64
  • 1,454
  • 1
  • 16
  • 26
  • thanks for the quick reply and suggestion! Not sure if makes any difference to your answer, but the actual "name" for the question is: name="b[question_9][]" and is part of a "multiselect" section where dropdown value options are now presented as "checkboxes", and a person CAN choose more than one. I would prefer to use radio buttons though & pass "Blue/Green"(ie "no preference") as a hidden value with whichever radio value is selected. Also, this question type is shared by other questions, so am concerned that will make it complicated to do in php file, without affecting other questions too. – Denny Feb 25 '13 at 23:01
  • gosh.... i THOUGHT that it would have to be comlicated, ie that since I AM using radio buttons, that I could ONLY have one value submitted for the "name", including if there was a hidden field. Hence, my thought that I somehow had to actually use checkboxes, which would allow for multiple values to be selected, and only use the radio buttons round about to assign the checked value to a checkbox behind the scene.... turns out that isn't the case lol, and the solution was indeed THAT simple. Thanks for your time and answer! – Denny Feb 26 '13 at 19:10