简体   繁体   中英

PHP $_POST get data array

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:

email1@email.com, email2@email.com, email3@email.com

How can I do that? is that possible?

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 email1@email.com, email2@email.com ...

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

<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.

Another example could be:

<input type="text" name="email[]" value="1">
<input type="text" name="email[]" value="2">
<input type="text" name="email[]" value="3">

<?php
     foreach($_REQUEST['email'] as $key => $value)
          echo "key $key is $value <br>";

will display

key 0 is 1
key 1 is 2
key 2 is 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM