PHP 将所有 $_POST[] 变量访问到一个数组中? [英] PHP access all $_POST[] variables into an array?
问题描述
如何捕获$_POST
VARIABLE 中的所有字段?成一个数组?
how to capture all fields in the $_POST
VARIABLE?
into an array?
$email = $_POST;
$emails = array_keys($email);
foreach($emails as $email) {
echo '$' . nl2br($email);
}
$emailadd = new email_save;
$emailadd->insert_email_into_database("POST VARIABLES GO HERE)
我正在尝试制作电子邮件列表、姓名和电子邮件地址,我如何捕获可以正常访问的所有变量,例如$_POST['email']
到一个数组中,以便我可以将它们添加到我的函数中的参数中?
I'm trying to make an email list, name and email address,
how do I capture all the variables that where posted that can normal be accessed like
$_POST['email']
into an array so i can add them to the arguments in my functions?
我的表单字段有 5 个字段.方法是POST.
My form field has 5 fields. The method is POST.
而不是写,
$email = mysql_real_escape_string($_POST['email']);
$firstname = mysql_real_escape_string($_POST['firstname']);
我正在尝试访问 $_POST[] 数组中的所有值;这样我就可以节省获取已发布变量的时间并将函数添加到这些变量中,例如 mysql_real_escape_string();
I'm trying to access all the values in the $_POST[] array;
So I can save time getting posted variables
and add functions to those variables like mysql_real_escape_string();
其他安全措施将被找出.
Other security measures will be figured out.
我希望能够将数组的引用添加到函数中,以便将它们添加到数据库中.
I want to be able to add a reffrence of the array to a function so I can add them to the database.
推荐答案
如果您想从 POSTed 表单中捕获列表,请使用数组语法技巧而不是枚举输入字段名称:
If you want to capture a list from a POSTed form, then use the array syntax trick instead of enumerated input field names:
<input type="email" name="emails[]">
<input type="email" name="emails[]">
<input type="email" name="emails[]">
这样你就不需要在 PHP 中猜测了,因为 emails[]
然后隐式地变成了一个数组:
This way you need no guessing in PHP, because emails[]
becomes an array implicitely then:
print_r($_POST["emails"]);
foreach ($_POST["emails"] as $email) {
对于数据库转义,只需使用:
For database-escaping just use:
$db_emails = array_map("mysql_real_escape_string", $_POST["emails"]);
// that's an array too
这篇关于PHP 将所有 $_POST[] 变量访问到一个数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!