如何从 $_POST 获取数据数组 [英] How to get an array of data from $_POST

查看:34
本文介绍了如何从 $_POST 获取数据数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个同名文本框的表单.我想从我的 PHP 代码中的所有文本框中获取数据.

I have a form with multiple textboxes with the same names. I want to get the data from all the textboxes in my PHP code.

这是我的代码.

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

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

我想要这样的结果:

email1@email.com、email2@email.com、email3@email.com

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

相反,我只从最后一个文本框中获取文本.

Instead I only get the text from the last textbox.

推荐答案

在元素名称中使用[]

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端返回一个数组:

will return an array on the PHP end:

$email = $_POST['email'];   

你可以implode()得到你想要的结果:

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, e.g. serializing the array or inserting them into a database! Just because they're in an array doesn't mean they are safe.

这篇关于如何从 $_POST 获取数据数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆