1

Sorry for the noob question. But I am stuck here.

This is my HTML form where the user-form div can be cloned to as many as possible. The #submit-form div has some hidden values which are common for all.

HTML -

 <div class="user-form">
    <input type="text" autocomplete="off" name="name[]"  >
    <input type="email"  autocomplete="off" name="mail[]"  >
 </div>

 <div class="user-form">
    <input type="text" autocomplete="off" name="name[]"  >
    <input type="email"  autocomplete="off" name="mail[]"  >
 </div>

 <div id="submit-form">
    <input type='hidden' name='refer_user_id'  value='<?php echo $refer_user_id ?>'>
    <input type='hidden' name='refer_user_email' value='<?php echo  $refer_user_email ?>'>
    <input type="submit"  value="Invite" />
    <input type="button" class="button" id="clonetrigger" value="Clone" />
</div>

I'm using ajax to submit the form. Basically I want to create accounts using the name and email fields. In PHP How do I use foreach to loop through the name and email fields so that I can create unique accounts?

My print_r($_POST); array looks like this.

Array
(
  [name] => Array
    (
        [0] => david
        [1] => Mark
        [2] => cindy
    )

  [mail] => Array
    (
        [0] => [email protected]
        [1] => [email protected]
        [2] => [email protected]
    )

  [refer_user_id] => 2
  [$refer_user_email] => [email protected]
)
1
  • 1
    Looks like if you used a regular for loop with an index (i.e. not foreach) you can match up the numerical indexes, right? Commented Dec 3, 2014 at 20:05

5 Answers 5

4

Create a loop with a number of iterations equal to the number of submitted name/email pairs, then use the loop counter to access the values for each user.

for ($i = 0; $i < count($_POST['name']); $i++) {
{
    $name = $_POST['name'][$i];
    $mail = $_POST['mail'][$i];
    // Process the new user
}
Sign up to request clarification or add additional context in comments.

1 Comment

don't count in the loop
3

go through one of the arrays with a foreach, use the key for the second array.

 foreach($_POST['name'] as $key =>$name ){
    $mail = $_POST[$key];
 }

Comments

2
foreach($_POST['name'] as $key => $val) {
  echo $val
}

foreach($_POST['mail'] as $key => $val) {
  echo $val
}

Easiest way to loop through those elements. You can reference the other elements with $_POST['refer_user_id']. While this works for the purposes of a foreach, the for loop posted above is more efficient, so I'd recommend using it.

http://php.net/manual/en/control-structures.foreach.php More reading on it here.

Comments

2

You can use array_combine function:

$data = array_combine($_POST['name'],$_POST['mail']);
foreach($data as $name=>$mail){
  print $name;
  //...
}

See array_combine.

Comments

1

You could also use the JavaScript that's auto-generating the form items to give them a name that would result in linking the php object. i.e.

 <div class="user-form">
    <input type="text" autocomplete="off" name="user[1][name]"  />
    <input type="email"  autocomplete="off" name="user[1][mail]"  />
 </div>

 <div class="user-form">
    <input type="text" autocomplete="off" name="user[2][name]"  />
    <input type="email"  autocomplete="off" name="user[2][mail]"  />
 </div>

Then you could loop through the pairs with foreach($_POST['user'] as $key=>$value) etc...

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.