开发者

Collect specific fields to an array

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="t开发者_如何学编程extfield" />
        <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.


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'
    ),
    ...
)


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


To convert XML to JSON you can try with:

  • Services_Json from PEAR: http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp
  • the Zend_Json component of the Zend Framework: http://framework.zend.com/manual/en/zend.json.xml2json.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜