1

I am creating a form that users can add extra fields to dynamically and every time a new input is created, it is named accountemails[type] with the type being different for each group.

I want to post this form and retrieve the data grouped by the type

For example, if I have 2 groups - group1 and group2 my inputs would be named:

name="accountemails[group1]"
name="accountemails[group2]"

And there could be multiple with the same group name

For the post in php, I tried

foreach($_POST["accountemails"] as $type => $email) {

}

Which I though would work and I could use $type as each group and then $email for each value but that only shows the last posted input value per group

4
  • What does var_dump($_POST["accountemails"]) tell you? ^^ Commented Dec 11, 2016 at 13:51
  • What exactly are you doing inside the foreach loop? And what values does $_POST["accountemails"] contain (see the comment above)? Commented Dec 11, 2016 at 13:52
  • You have to discriminate the fields per array in HTML. See stackoverflow.com/q/9073690/498457 Commented Dec 11, 2016 at 14:01
  • so i have posted g1, g2 and o1 and the var dump shows array(4) { ["general"]=> string(2) "g2" ["orders"]=> string(2) "o1" ["renewals"]=> string(0) "" ["accounts"]=> string(0) "" } Commented Dec 11, 2016 at 14:31

2 Answers 2

1

To elaborate on Benoti's answer, it sounds like your overwriting your $_POST['accountemails'] data when a new value is added. Add the group data to an array.

$_POST['accountemails']['group1'] [] = "Group 1 - 1"; //Emulates html form: name = accountemails[group1][] with value of: Group 1 - 1
$_POST['accountemails']['group1'] [] = "Group 1 - 2"; //Emulates html form: name = accountemails[group1][] with value of: Group 1 - 2

$_POST['accountemails']['group2'] [] = "Group 2 - 1"; //Emulates html form: name = accountemails[group2][] with value of: Group 2 - 1
$_POST['accountemails']['group2'] [] = "Group 2 - 2"; //Emulates html form: name = accountemails[group2][] with value of: Group 2 - 2

foreach ($_POST as $v) {

    foreach ($v['group1'] as $email) {

        echo "Group 1 Email: " . $email;
        echo "<br>";
    }

    foreach ($v['group2'] as $email) {

        echo "Group 2 Email: " . $email;
        echo "<br>";     
    }
}

Output

Group 1 Email: Group 1 - 1
Group 1 Email: Group 1 - 2
Group 2 Email: Group 2 - 1
Group 2 Email: Group 2 - 2
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to add multiple form input with the same name, you need to add double brackets at the very last end of the name.

 name="accountemails[group1][]"
 name="accountemails[group2][]"

Then if the user adds extra-fields for group1, each new accountemails in each different group will be added to an array that you'll be able to retrieve in your foreach loop.

13 Comments

The indexes (group1/group2) are different, so using name="accountemails[group1]"/name="accountemails[group2]" (without the extra brackets) is valid and will submit both fields into the $_POST['accountemails'] array. You're essentially making it another dimention wide when adding [] at the end, which isn't needed.
@Qirel but OP has stated that there could be multiple records with the same group name e.g. [group1]. So I'm guessing that OP is attempting to add data to the array of accountemails[group1] or accountemails[group2] which means this answer above would be correct as all OP is doing at the moment is overwriting ['group1'] or ['group2'] when a new value has been added.
@Qirel Just to clarify, I'm not trying cause a war :-) I'm just curious because this would of been my logic for resolving. Obviously if I'm wrong then put me straight.
this is what my form looks like: s27.postimg.org/fk54dsm6r/… - so each input would be accountemails[general][] and accountemails[orders][]
@Kitson88 Who said anything about a war? ;-) We're all friendly here! You might very well be right, though - I think I might've miss-interpreted the problem (and missed entirely the "And there could be multiple with the same group name" part of OPs question!).
|

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.