0

I have a bit of a headache here. My problem is as follows. I have three sites that I e-mail items to, all three sites have a couple of default mail addresses. so on my form i create inputs with the existing addresses and allow the user to create more dynamically. i might end up with the following

site 1 - address 1
site 1 - address 2

site 2 - address 1
site 2 - address 2
site 2 - address 3

etc

my inputs look like <input type="text" name="email[]"/>

i need to pass the site identifier and the inputted address to my php script and loop through it to add the list to the database

4
  • If you know that you only have 3 sites, why not name="site1email[]", name="site2email[]", and name="site3email[]"? Commented Aug 6, 2014 at 20:18
  • site items are dynamic, so could be any number at the end of the day, with associated addresses Commented Aug 6, 2014 at 20:19
  • So when you said "I have three sites that I e-mail items to", you really mean "I have a dynamic number of sites that I e-mail items to"? Commented Aug 6, 2014 at 20:20
  • You could also do name="email[site1][]"/name="email[site2][]"/etc Commented Aug 6, 2014 at 20:21

2 Answers 2

2

Let's assume a basic form like this:

<form method="post">
    <input type="text" name="email[]" />
    <input type="text" name="email[]" />
</form>

Your email fields would appear in PHP as the following:

$_POST['email'][0]
$_POST['email'][1]

As you can see, all email fields are neatly stored into the array $_POST['email']. PHP Does this automatically when appending [] to your input field names.

You can also nest them even deeper:

<form method="post">
    <input type="text" name="email[1][]" />
    <input type="text" name="email[1][]" />
    <input type="text" name="email[2][]" />
    ...
</form>

Now they would show up in $_POST['email'][1][...], $_POST['email'][2][...] and so on.

Sign up to request clarification or add additional context in comments.

Comments

1

You can specify the array key and also make it two dimensional:

name="site[1][]"
name="site[1][]"
name="site[2][]"
name="site[2][]"

Then loop through and use key and value:

foreach($_POST['site'] as $site => $addresses) { // $site is the number and $addresses is an array
    $address_list = implode(',', $adresses);     // or loop $addresses or whatever
}

There are a lot of possibilities depending on how is easiest to structure and access it in your particular case.

3 Comments

so if i understand this correctly i would have 'code' name="email[siteid][]". or as i will have it name="email[1][]".
Yes, that will work. I used site because the number is the site number and it made more sense.
to access it in php i use echo $email[siteid][offest]. ;) so i managed to figure it out from what you said, thank you so much

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.