tags:

views:

58

answers:

2

I'm trying to do a multiple textbox with the same names.
Here is my code.

HTML

Email 1:<input name="email" type="text"><br>
Email 2:<input name="email" type="text"><br>
Email 3:<input name="email" type="text"><br>


PHP

$email = $_POST['email'];
echo $email;

I wanted to have a results like this:

[email protected], [email protected], [email protected]

How can I do that? is that possible?

+13  A: 

Using [] in the element name

Email 1:<input name="email[]" type="text"><br>
Email 2:<input name="email[]" type="text"><br>
Email 3:<input name="email[]" type="text"><br>

will return an array on the PHP end:

$email = $_POST['email'];   

you can implode() that to get the result you want:

echo implode(", ", $email); // Will output [email protected], [email protected] ...

Don't forget to sanitize these values before doing anything with them, e.g. serializing the array or inserting them into a database! Just because they're in an array doesn't mean they are safe.

Pekka
Thank you for a very good reply.
Jordan Pagaduan
+3  A: 
<input name="email[]" type="text">
<input name="email[]" type="text">
<input name="email[]" type="text">
<input name="email[]" type="text">

$_POST['email'] will be an array.

Piskvor