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

Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52
charlie
  • 415
  • 4
  • 35
  • 83
  • What does `var_dump($_POST["accountemails"])` tell you? ^^ – moonwave99 Dec 11 '16 at 13:51
  • What exactly are you doing inside the `foreach` loop? And what values does `$_POST["accountemails"]` contain (see the comment above)? – Qirel Dec 11 '16 at 13:52
  • You have to discriminate the fields per array in HTML. See http://stackoverflow.com/q/9073690/498457 – Christoph Grimmer Dec 11 '16 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) "" } ` – charlie Dec 11 '16 at 14:31

2 Answers2

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.

Benoti
  • 2,170
  • 2
  • 14
  • 15
  • 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 Dec 11 '16 at 14:20
  • 1
    @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. – Kitson88 Dec 11 '16 at 14:40
  • @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. – Kitson88 Dec 11 '16 at 14:44
  • this is what my form looks like: https://s27.postimg.org/fk54dsm6r/Screen_Shot_2016_12_11_at_14_45_59.png - so each input would be accountemails[general][] and accountemails[orders][] – charlie Dec 11 '16 at 14:46
  • P.S. I have added [] to the end of the input names – charlie Dec 11 '16 at 14:47
  • 1
    @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!). – Qirel Dec 11 '16 at 14:49
  • @Qirel *We're all friendly here!* - I wish that was the case :-P Cheers for getting back to me. – Kitson88 Dec 11 '16 at 14:53
  • Yes we're all here to help :-) I only give the answer as how I understand the problem and how I did it many time in this case. – Benoti Dec 11 '16 at 14:54
  • Did you see my screenshot? How do I handle the POST data in PHP? – charlie Dec 11 '16 at 14:56
  • @charlie See comments above and Benoti's answer. Add `[]` to the end of your form array to add elements to your `$_POST` array. At the moment, it sounds like your overwriting your data as the values are not being added to a sub-array. – Kitson88 Dec 11 '16 at 14:59
  • Yes i saw your screenshots, the foreach loop need to be different. I didn't correct it. Try to loop in each group, -> foreach($accountdetails['group1'] as $email) – Benoti Dec 11 '16 at 14:59
  • @Benoti what is `$accountdetails` ? – charlie Dec 11 '16 at 15:00
  • i have tried this `foreach($_POST["accountemails"] as $type)` and then inside here - `foreach($type as $t)` – charlie Dec 11 '16 at 15:01
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
Kitson88
  • 2,889
  • 5
  • 22
  • 37