PHP | 通过 $_POST[] 获取输入名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13377850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP | Get input name through $_POST[]
提问by user1178560
HTML example:
HTML 示例:
<form method="post" id="form" action="form_action.php">
<input name="email" type="text" />
</form>
User fills input field with: [email protected]
用户填写输入字段:[email protected]
echo $_POST['email']; //output: [email protected]
The name and value of each input within the form is send to the server. Is there a way to get the name property? So something like..
表单中每个输入的名称和值被发送到服务器。有没有办法获得 name 属性?所以像..
echo $_POST['email'].name; //output: email
EDIT: To clarify some confusion about my question;
编辑:澄清对我的问题的一些困惑;
The idea was to validate each input dynamically using a switch. I use a separate validation class to keep everything clean. This is a short example of my end result:
这个想法是使用开关动态验证每个输入。我使用一个单独的验证类来保持一切干净。这是我最终结果的一个简短示例:
//Main php page
if (is_validForm()) {
//All input is valid, do something..
}
//Separate validation class
function is_validForm () {
foreach ($_POST as $name => $value) {
if (!is_validInput($name, $value)) return false;
}
return true;
}
function is_validInput($name, $value) {
if (!$this->is_input($value)) return false;
switch($name) {
case email: return $this->is_email($value);
break;
case password: return $this->is_password($value);
break;
//and all other inputs
}
}
Thanks to raina77ow and everyone else!
感谢raina77ow和其他人!
回答by raina77ow
You can process $_POST in foreachloop to get both names and their values, like this:
您可以在foreach循环中处理 $_POST以获取名称及其值,如下所示:
foreach ($_POST as $name => $value) {
echo $name; // email, for example
echo $value; // the same as echo $_POST['email'], in this case
}
But you're not able to fetch the name of property from $_POST['email']value - it's a simple string, and it does not store its "origin".
但是您无法从$_POST['email']value 中获取属性的名称- 它是一个简单的字符串,并且不存储其“来源”。
回答by DonCallisto
foreach($_POST as $key => $value)
{
echo 'Key is: '.$key;
echo 'Value is: '.$value;
}
回答by Adam
If you wanted to do it dynamically though, you could do it like this:
如果你想动态地做,你可以这样做:
foreach ($_POST as $key => $value)
{
echo 'Name: ', $key, "\nValue: ", $value, "\n";
}
回答by Adam
Loop your object/arraywith foreach:
循环object/array使用foreach:
foreach($_POST as $key => $items) {
echo $key . "<br />";
}
Or you can use var_dumpor print_rto debug large variables like arraysor objects:
或者您可以使用var_dumporprint_r来调试大变量,例如arraysor objects:
echo '<pre>' . print_r($_POST, true) . '</pre>';
Or
或者
echo '<pre>';
var_dump($_POST);
echo '</pre>';
回答by Nikhil Girraj
Actually I found something that might work for you, have a look at this-
其实我找到了一些可能对你有用的东西,看看这个-
http://php.net/manual/en/function.key.php
http://php.net/manual/en/function.key.php
This page says that the code below,
这个页面说下面的代码,
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'<br />';
}
next($array);
}
?>
would give you the output,
会给你输出,
fruit1
fruit4
fruit5
水果1
水果4
水果5
As simple as that.
就如此容易。
回答by Mikeys4u
Nice neat way to see the form names that are, or will be submitted
查看已提交或将要提交的表单名称的好方法
<?php
$i=1;
foreach ($_POST as $name => $value) {
echo "</b><p>".$i." ".$name;
echo " = <b>".$value;
$i++;
}
?>
Just send your form to this script and it will show you what is being submitted.
只需将您的表单发送到此脚本,它就会向您显示正在提交的内容。
回答by xelber
current(array_keys($_POST)) should give you what you are looking for. Haven't tested though.
current(array_keys($_POST)) 应该给你你正在寻找的东西。不过没测试过。
回答by dognose
You can use a foreach Loop to get all values that are set.
您可以使用 foreach 循环来获取所有设置的值。
foreach ($_POST AS $k=>$v){
echo "$k is $v";
}
Your example
你的榜样
echo $_POST['email'].name; //output: email
wouldnt make sense, since you already know the name of the value you are accessing?
没有意义,因为您已经知道要访问的值的名称?

