我试图通过遍历在php中调用函数时生成的行来获取正确的结果,但是我只得到一行而不是所有行。下面的代码我有。
<form method="post" action="done.php">
<div id="add_passenger">
<div class="fields">
<input type="text" name="field[email]" />
<input type="text" name="field[name]" />
</div>
<button type="button" onclick='add_field_row();' ">Add Email</button>
<input type="submit" value="submit" />
</div>
</form>
$dynamicEmailFields = $_POST['field']
<?php
foreach($test as $key => $fields) {
//Here i want to output the field name and value depending on the
number of row generated when calling add_field_row() For instance:
email1: email1@mail.com
name1: john doe
email2: email1@mail.com
name2: john doe
//With this result from the loop it means that there were 2 rows generated.
}
?>
答案 0 :(得分:1)
您需要输入数组。这将是最简单的循环:
<input type="text" name="field[0][email]" />
<input type="text" name="field[0][name]" />
<input type="text" name="field[1][email]" />
<input type="text" name="field[1][name]" />
然后:
foreach($_POST['field'] as $field) {
echo 'email: ' . $field['email'] . '<br/>';
echo 'name: ' . $field['name'] . '<br/>';
}