1

I have a form with a series of radio buttons and checkboxes used to tally up a cost before sending the user to a payment gateway.

The inputs are nothing unusual, except I've used the 'value' property for the associated price as when any input is changed, a table is edited to show chosen options, their associated prices and then the total.

<input type="checkbox" id="cb1" value="100" name="cb1">
<input type="checkbox" id="cb2" value="200" name="cb2">
<input type="radio" id="rd1" value="500" name="rdo">
<input type="radio" id="rd2" value="500" name="rdo">

I was slightly stupid and didn't think ahead as to how I was then going to actually process the completed form when submitted in terms of determining what the user has selected.

Sending the final total through is fine as that's stored in a readonly input. However, I also need to save to the database which checkboxes have been selected and which radio is selected.

The checkboxes are manageable as they only have a value in $_POST if they are ticked so I can cycle through them all and only keep the ones whose value isn't empty.

Any thoughts for how to handle the radio buttons? Re-writing the form is a last resort at this stage if possible to not do so.

6
  • 1
    I like how your question is precise and you have an understanding of what's going on with your code. Now for the radio buttons, normally submit would pass the selected radio's value, however from the above I see that both have same value, so do you need to pass the id of it instead? Commented Mar 19, 2014 at 12:32
  • Both radio buttons have the same value. Intentional? Commented Mar 19, 2014 at 12:34
  • 1
    "Sending the final total through is fine as that's stored in a readonly input" sounds extremely dangerous to me - you should calculate total server side Commented Mar 19, 2014 at 12:42
  • Yea i agree , You should not take such risks, Your smallest carelessness may become the biggest failure Commented Mar 19, 2014 at 12:59
  • @ICanHasCheezburger: Thank you. Ideally yes, except id isn't passed in get or post. - LShetty: Yes. (The actual form is larger than this, there are 12 radios, some of the values are the same, some aren't. - user574632: I know but this is only being used within a small local community - ultimately if any one wants to fiddle it doesn't do any thing until they actually pay and we'll spot any abnormalities in the transactions (as there won't be that many in the grand scheme). Commented Mar 19, 2014 at 13:05

2 Answers 2

1

here is a solution in jquery

what you have to do is ADD A CLASS NAME to those radiobuttons or checkboxes which you have to make a list of (in case they are checked), this class name will help you to manipulate them with a single function please c the below code and modify accordingly , hope this helps you

     <input class="somename" type="checkbox" id="cb1" value="100" name="cb1" >
     <input class="somename" type="checkbox" id="cb2" value="200" name="cb2">
     <input class="somename" type="radio" id="rd1" value="500" name="rdo">
     <input class="somename" type="radio" id="rd2" value="500" name="rdo">
     <input type="button" id="finalClick" > // something like your submit button

Jquery

    $('#finalClick').bind("click",function(e){
       var list="";
       $(".somename").each(function(e){
           if($(this).is(':checked'))
           list = list + $(this).attr('id')+"  ";
       });
     alert(list); // You can save this in some hidden field so that you can use it in your code
   });

Fiddle

http://jsfiddle.net/AmarnathRShenoy/zydS8/

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate the warnings made earlier about storing in a hidden field but in this instance it's 'safe enough' so have gone with this route. Thanks for the help all.
0

For check boxes, you can store the values in an array so you aren't dealing with an bunch of different variable names. (The ID attribute will need to be different if you are accessing them via client side code.)

<input type="checkbox" id="cb1" value="100" name="cb[1]">
<input type="checkbox" id="cb2" value="200" name="cb[2]">

When your form is sent, only the values in your list that are checked should show in the resulting array. Accessing the values via $_POST['cb'] and printing it out should produce something like this:

Array
(
    [2] => 200
)

Where the key 2 represents that the check box with name="cb[2]" was checked.

As for the radio buttons, I your values for both buttons are the same. This may be by design, however radio buttons are generally used for select a single value from a set of different values. When sending a radio button element, only the value that was selected will be passed to the script under the element's name. In your case you will get a value of 500 in $_POST['rdo'] no matter which one is selected. Therefore you don't really have a clear way of telling which radio button was selected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.