How can I group form elements

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 whether it is checked inside or out, if outside I checked the multiplication points for a given text input by 1.5. BTW it needs to be calculated in PHP.

How can i do this?

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 ) ) ) 

Higher result print_r ($ _ POST)

Now ho me compare / pare Array of points with array Group like this:

are points [0] "connected" to group [0], etc.?

+23
source share
3 answers

As it turned out, you can group fields using HTML forms. Check out this code here: (especially pay attention to 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 populating anything, this will give a POST array as follows:

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

Hope this answers your question, this is a neat little trick that I didn't see when many others were discussing. It should be noted that you need to manually specify the identification number in any set of brackets. You can use [] only as the last set of brackets.

+65
source

I am expanding this answer because it took me a while to track the PHP code that will parse the data from the form.

Using this method in HTML, you get an array of key-value pairs.

  <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!

+2
source

You just need to catch that it is returned in the $_POST variable, and process it. If you execute var_dump($_POST) after filling out the form, you should better understand what to do.

0
source

All Articles