getting radiobuttonlist values

So I’m a newbie to Yii, as I’ve mentioned in previous posts. Here’s my pickle, I’m using 3 seperate radiobuttonlists on a form. They are NOT active radiobuttonlists. The form represents a model obviously, this model is called vote. None of the fields on this form map to vote model attributes. On the form there are questions and 3 possible answers for each question. the text for these questions and answers is pulled from an xml file. Each answer is displayed with a radio button, and each answer needs to have a weight attached to it. I’ve used hidden fields which pull integer values from the same xml file. So the form is laid out roughly like this

Question1: is this a good product? // question 1 of 3

radiobutton1: I think so

weight:10 // weights are hidden fields, integer value pulled from xml file.

radiobutton2: not really

weight:7

radiobutton3: it’s ok

weight:4

Question2: Would you use it again? // question 2 of 3

radiobutton1: yes

weight:10 // weights are hidden fields, integer value pulled from xml file.

radiobutton2:no

weight:7

radiobutton3: maybe

weight:4

Question3: Why? // question 3 of 3

radiobutton1: didn’t do its job

weight:10 // weights are hidden fields, integer value pulled from xml file.

radiobutton2: I like this other product

weight:7

radiobutton3: I’d rather be fishing

weight:4

so what I need to do is, when the submit button is clicked I need to determine which radio buttons were selected on the 3 different radiobuttonlists. If, say the user selected answer 1 for all 3 questions, I need to add up the weights of those hidden fields associated with those answers and calculate a total. This info will then be used to create a certain type of vote, if that makes sense. Bare in mind the hidden fields and radiobuttons are not linked in anyway, I’d have to do that logic myself obviously. I hope this makes sense. if anyone can help I’d be eternally grateful.

Thanks in advance

A few comments:

  • I don’t see anything specific to Yii in your question, this is just plain HTML/PHP.

  • Please keep in mind that you receive the weights from the client, so they can be altered.

    A malicious user can modify the source of your form, then post his answer with custom weight.

    If you want to keep this data flow, then you should add a hash to your form (see below for a hash example).

Now for the main answer, a solution would be to set the values of each radio button to its weight. Then summing the values submitted is easy. But in this case, you really need to make sure the values aren’t tampered. Here is the principle for this:

In the view


<label><input type="radio" name="param[X]" value="50_<?php echo md5("key and 50"); ?>" /> A</label>

<label><input type="radio" name="param[X]" value="100_<?php echo md5("key and 100"); ?>" /> B</label>

In the controller:


$total = 0;

foreach($_POST["param"] as $name => $rawvalue) {

    list ($weight, $hash) = explode("_", $rawvalue);

    if ($hash !== md5("key and " . $value)) {

        throw new CHttpException(500, "A value was altered");

    }

    $total += $weight;

}



Thanks a lot Francois, appreciate your input