Reputation: 239
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
Upvotes: 0
Views: 1550
Reputation: 78994
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.
Upvotes: 1
Reputation: 16061
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.
Upvotes: 2