如何在PHP中将变量作为$ _POST键传递?

Die*_*xel 6 php variables

如何在PHP中将变量作为$ _POST数组键值传递?还是不可能?

$test = "test";
echo $_POST[$test];
Run Code Online (Sandbox Code Playgroud)

谢谢

Qua*_*unk 15

如果我找对你,你想通过post将一个变量从一个php文件传递给另一个.这可以通过多种方式确定.

1.使用HTML表单

<form action="target.php" method="post">
  <input type="text" name="key" value="foo" />
  <input type="submit" value="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

如果你点击提交按钮,$_POST['key']target.php将包含'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); 
Run Code Online (Sandbox Code Playgroud)

1中的相同,$return并将包含由...生成的所有输出target.php.

3.通过AJAX(jQuery(JavaScript))

<script>
$.post('target.php', {key: 'foo'}, function(data) {
  alert(data);
});
</script>
Run Code Online (Sandbox Code Playgroud)

2.中的相同,但现在data包含来自的输出target.php.


Dim*_*nov 9

$_POST['key'] = "foo";
echo $_POST['key'];
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,你想设置一个$_POST密钥.


JCO*_*611 5

是的,是的,您可以:

$postName = "test";
$postTest = $_POST[$postName];
$_POST["test"] == $postTest; //They're equal
Run Code Online (Sandbox Code Playgroud)