views:

198

answers:

3

Hi everyone,

Each div with the class "row" is added upon request from the user, to be able to add multiple items at once. So now is the question how I'll collect all the forms in to an array that PHP can read (like JSON for instance). I'll guess that there's already some easy and effective way of doing this?

<div class="container">
    <div class="row">
        <input type="text" name="value1" id="textfield" />
        <input type="text" name="value2" id="textfield" />
        <input type="text" name="value3" id="textfield" />
    </div>

</div>

Here's what I would like to achieve out of the shown example:

array( 
    array ('value1' => '',
           'value2' => '',
           'value3' => '')
);

Thanks!

Update: The form will be handled with PHP and it would be super to be able to do something like a foreach loop on the specific container-div content.

+4  A: 

Give each 'group' of inputs the same name, then add square brackets to the end

<div class="container">
    <div class="row">
        <input type="text" name="value1[]" id="textfield" />
        <input type="text" name="value2[]" id="textfield" />
        <input type="text" name="value3[]" id="textfield" />
    </div>
    <div class="row">
        <input type="text" name="value1[]" id="textfield" />
        <input type="text" name="value2[]" id="textfield" />
        <input type="text" name="value3[]" id="textfield" />
    </div>
</div>

When you post the form, your php $_POST variable will then contain arrays for value1, value2 and value2:

var_dump($_POST); // array('value1' = array(...

You can then iterate through to 'regroup' the rows within PHP (but first, i'd change the field names to field1 etc rather than value1):

$rows = array(); // set up an empty array to hold your rows

// loop through each POST var
foreach($_POST AS $key=>$field) {
    if(is_array($field)) {
        foreach($field AS $rowIndex=>$fieldValue) {
            $rows[$rowIndex][$field] = $fieldValue; // put the value in a the array by row, field
        }
    }
}

var_dump($rows);

This would give:

array(
    [0] => array(
        'field1' => 'value1',
        'field2' => 'value2',
        'field3' => 'value3'
    ),
    [1] => array(
        'field1' => 'value1',
        'field2' => 'value2',
        'field3' => 'value3'
    ),
    ...
)
adam
Hi Adam. How would I know from the PHP side which arrays that are posted? From what I can see, each "row" will not be grouped in any way?
Industrial
@Industrial - the rows are grouped by the index of each array (starting with 0). I'll update my answer
adam
What I mean is that each row is grouped, but I would like to collect all the rows into one big array so I can do a foreach in PHP on the big array.
Industrial
Whoa, give me a chance to update! There, done.
adam
Hi Adam, that is great! Is this possible to do this without the use of var_dump? I would need to group all the rows into one array That I know the name on, since I would like to pass multiple arrays through the POST data
Industrial
You don't need var_dump - i was only using it to illustrate the output of the variable, so you'd know what you were getting
adam
Yep, I can see that now. But what approach should I use if I would need to have multiple arrays sent through the POST function? - There's must be a good way to separate them from eachother then, in this case from which container-DIV they belong to, if I need to have multiple arrays for different data types?
Industrial
A: 
<div class="container">
    <div class="row">
        <input type="text" name="value[0][value1]" class="textfield" />
        <input type="text" name="value[0][value2]" class="textfield" />
        <input type="text" name="value[0][value3]" class="textfield" />
    </div>
    <div class="row">
        <input type="text" name="value[1][value1]" class="textfield" />
        <input type="text" name="value[1][value2]" class="textfield" />
        <input type="text" name="value[1][value3]" class="textfield" />
    </div>
</div>

Changed id to class as reusing the same id is invalid HTML.

Tgr
Thanks, I am aware of that, but have overlooked it when putting together the example in my original post.
Industrial
I mean, I answered your question *and* changed id to class :) Check the element names, they will give the desired effect.
Tgr
A: 

To convert XML to JSON you can try with:

nuqqsa