0

I'm trying to get the right results from looping through rows generated when calling a function in php but i only get one row generated not all the rows. Below code of what i have.

<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: [email protected]
         name1: john doe
         email2: [email protected]
         name2: john doe

       //With this result from the loop it means that there were 2 rows generated.  
    }
?>
3
  • What is the actual code look like? Commented Jul 6, 2018 at 18:23
  • 1
    You would need to modify the JS, but inputs are overwriting each other, you would need name="field[email][]" and name="field[name][]" OR name="field[0][email]" and name="field[0][name]" Commented Jul 6, 2018 at 18:23
  • AbraCadaver - got it and then how would you loop. Commented Jul 6, 2018 at 18:30

1 Answer 1

1

You need an array of inputs. This would be the easiest to loop:

<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]" />

Then:

foreach($_POST['field'] as $field) {
    echo 'email: ' . $field['email'] . '<br/>';
    echo 'name:  ' . $field['name']  . '<br/>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.