获取帖子变量的名称 [英] get name of a post variable

查看:26
本文介绍了获取帖子变量的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
PHP $_POST 打印变量名和值

我有一个表单(无论字段数如何).这个表单将发送 $_POST 数据.我想返回每个 $_POST 变量值和名称.

我想做这样的事情:

foreach($_POST as $field){//****一些代码*****//}

以某种方式显示字段和值,如下所示:

<块引用><块引用>

姓名:西蒙塔奇
邮箱:example@ymail.com

换句话说:

如果我有一个帖子变量:$_POST['city']='las vegas'

我想知道如何获取变量的名称:'city'.

解决方案

$_POST 填充为关联数组,因此您可以这样做:

foreach ($_POST as $name => $val){echo htmlspecialchars($name . ': ' . $val) ."
";}

另外,如果你只对字段名感兴趣,你可以使用 array_keys($_POST); 返回一个包含 $_POST.您可以使用这些键来引用 $_POST 中的值.

foreach (array_keys($_POST) as $field){回声 $_POST[$field];}

Possible Duplicate:
PHP $_POST print variable name along with value

I have a form (whatever number of fields).. this form will send $_POST data.. I want to return every $_POST variable value and name.

I wanna do soemthing like this :

foreach($_POST as $field){
    //****some code*****//
}

in a way that will display the fields and values like this:

name : Simo Taqi
email : example@ymail.com

in other words :

if I have a post variable : $_POST['city']='las vegas'

I want to know how to get the name of the variable : 'city'.

解决方案

$_POST is populated as an associative array, so you can just do this:

foreach ($_POST as $name => $val)
{
     echo htmlspecialchars($name . ': ' . $val) . "
";
}

Additionally, if you're just interested in the field names, you can use array_keys($_POST); to return an array of all the keys used in $_POST. You can use those keys to reference values in $_POST.

foreach (array_keys($_POST) as $field)
{
    echo $_POST[$field];
}

这篇关于获取帖子变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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