29

I got this form:

<form method="post" action="" accept-charset="utf-8"> 
<p>
  <label>first_field</label><br />
  <input type="text" id="first_field" name="points[]" /><br />
  <input type="radio" value="inside" name="group_1" checked /><br />
  <input type="radio" value="outside" name="group_1"><br />
</p>
<p>
  <label>second_field</label><br />
  <input type="text" id="second_field" name="points[]" /><br />
  <input type="radio" value="inside" name="group_2" checked /><br />
  <input type="radio" value="outside" name="group_2"><br />
</p>
</form>

What i want to accomplish is to check if inside or outside is checked, if outside i checked the multiply points for the given text input by 1,5. BTW this needs to be calculated in PHP.

How can I do that?

UPDATE

Array
(
[bonus] => Array
    (
        [points] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
                [5] => 0
                [6] => 0
                [7] => 0
                [8] => 0
                [9] => 0
                [10] => 0
                [11] => 0
                [12] => 0
                [13] => 0
                [14] => 0
            )

        [group] => Array
            (
                [0] => inside
                [1] => outside
                [2] => outside
                [3] => inside
                [4] => inside
                [5] => inside
                [6] => inside
                [7] => inside
                [8] => outside
                [9] => inside
                [10] => inside
                [11] => inside
                [12] => outside
                [13] => inside
                [14] => inside
            )

    )

)

Above is the result of print_r($_POST)

Now ho do I compare/pare The points array with the Group array so:

points[0] gets "connected" to group[0] etc.?

2
  • 1
    For more semantic mark up, use the <fieldset> tag instead of those <p> tags, and use <legend> instead of <label> for those titles.
    – Mild Fuzz
    Apr 27, 2011 at 9:41
  • Definitely don't use label to label groups of fields. A label is associated with a single field (which either needs to be a child of the label, or referenced with a for attribute)
    – Quentin
    Apr 27, 2011 at 9:45

3 Answers 3

83

As it turns out, you can group fields using HTML forms. Check out this code here: (specifically note the name attributes)

<form method="post" action="" accept-charset="utf-8">
<p>
  <label>first_field</label><br />
  <input type="text" id="first_field" name="field[1][points]" /><br />
  <input type="radio" value="inside" name="field[1][group]" checked /><br />
  <input type="radio" value="outside" name="field[1][group]"><br />
</p>
<p>
  <label>second_field</label><br />
  <input type="text" id="second_field" name="field[2][points]" /><br />
  <input type="radio" value="inside" name="field[2][group]" checked /><br />
  <input type="radio" value="outside" name="field[2][group]"><br />
</p>
</form>

Without filling anything in, this will yield a POST array like this:

Array
(
    [field] => Array
        (
            [1] => Array
                (
                    [points] => 
                    [group] => inside
                )

            [2] => Array
                (
                    [points] => 
                    [group] => inside
                )

        )
)

I hope this answered your question, it's a neat little trick I haven't really seen many others discuss. One thing to note is that you'll need to manually specify an ID number in that any set of brackets. You can only use [] as the last set of brackets.

1
  • 2
    Thank you Dominic, that was what I where looking for.
    – andkjaer
    Apr 28, 2011 at 9:20
2

I'm expanding on this answer because it took me a while to track down the PHP code that would parse the data from the form.

Using this technique in HTML will result in a key-value pair array.

  <input type="text" id="first_field" name="field[1][points]" /><br />
  <input type="radio" value="inside" name="field[1][group]" checked /><br />
  <input type="radio" value="outside" name="field[1][group]"><br />

This is how I used PHP to parse the array.

foreach ($_POST as $record => $detail) {
// The submit button from my HTML form was POSTing data
// so I used an if statement to remove it from the result set
if(empty($firstRow))
{
    $firstRow = 1;
}
else
{
    // since $detail is still an array, you have to loop through it again
    foreach ($detail as $key => $value) {
            echo $key."<br/>";
            // $key would contain the key value (1 or 2)
            echo $value['points']."<br/>";
            echo $value['group']."<br/><br/>";
    }
}

}

Hope this answer helps!

0

You just need to catch what it coming back in the $_POST variable, and process it. If you do a var_dump($_POST) after completing your form, you should have a better idea of what to do.

1
  • or print_r($_post) with a <pre> tag :)
    – littlechad
    Apr 27, 2011 at 10:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.