【发布时间】:2011-12-23 05:42:23
【问题描述】:
如何在 PHP 中将变量作为 $_POST 数组键值传递?还是不可能?
$test = "test";
echo $_POST[$test];
谢谢
【问题讨论】:
-
您演示的内容应该可行,但我不确定您要做什么?
-
天哪,它有效。另一段代码把它扔掉了。
如何在 PHP 中将变量作为 $_POST 数组键值传递?还是不可能?
$test = "test";
echo $_POST[$test];
谢谢
【问题讨论】:
正如你所说的那样工作......
例子:
// create an array of all the GET/POST variables you want to use
$fields = array('salutation','fname','lname','email','company','job_title','addr1','addr2','city','state',
'zip','country','phone','work_phone');
// convert each REQUEST variable (GET, POST or COOKIE) to a local variable
foreach($fields as $field)
${$field} = sanitize($_POST[$field]);
?>
根据 cmets 和 downvotes 更新 ....
我不是,正如下面在 cmets 循环所有数据并添加到变量中所建议的那样 - 我正在循环预先确定的变量列表并将它们存储在变量中......
我改变了获取数据的方法
【讨论】:
mysql_user 而我不会阅读它
$_GET、$_POST 或$_COOKIE 以保持一致性,但是如果生成的变量是根据仅服务器端的数组创建的,那么与局部变量损坏相关的安全问题在哪里?
如果我没听错的话,您想通过 post 将变量从一个 php 文件传递到另一个。这肯定可以通过多种方式实现。
1.使用 HTML 格式
<form action="target.php" method="post">
<input type="text" name="key" value="foo" />
<input type="submit" value="submit" />
</form>
如果您单击提交按钮,target.php 中的$_POST['key'] 将包含'foo'。
2。直接来自 PHP
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: text/html\r\n",
'content' => http_build_query(array('key' => 'foo'))
),
));
$return = file_get_contents('target.php', false, $context);
与 1. 中的内容相同,$return 将包含target.php 产生的所有输出。
3.通过 AJAX (jQuery (JavaScript))
<script>
$.post('target.php', {key: 'foo'}, function(data) {
alert(data);
});
</script>
与 2. 中的内容相同,但现在 data 包含来自 target.php 的输出。
【讨论】:
$_POST['key'] = "foo";
echo $_POST['key'];
如果我理解正确,你想设置一个$_POST 键。
【讨论】:
是的,是的,你可以:
$postName = "test";
$postTest = $_POST[$postName];
$_POST["test"] == $postTest; //They're equal
【讨论】: