Mayren Martinez
Mayren Martinez

Reputation: 105

Unable to generate values from dynamic fields created PHP|Javascript

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

Upvotes: 0

Views: 26

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

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/>';
}

Upvotes: 1

Related Questions