php 获取通过 POST 发送的所有变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8207488/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:09:22  来源:igfitidea点击:

Get all variables sent with POST?

phphttp-post

提问by lisovaccaro

I need to insert all variables sent with post, they were checkboxes each representing an user.

我需要插入随 post 发送的所有变量,它们是每个代表一个用户的复选框。

If I use GET I get something like this:

如果我使用 GET 我得到这样的东西:

?19=on&25=on&30=on

I need to insert the variables in the database.

我需要在数据库中插入变量。

How do I get all variables sent with POST? As an array or values separated with comas or something?

如何使用 POST 发送所有变量?作为用逗号或其他东西分隔的数组或值?

回答by Code Magician

The variable $_POSTis automatically populated.

该变量$_POST会自动填充。

Try var_dump($_POST);to see the contents.

尝试var_dump($_POST);查看内容。

You can access individual values like this: echo $_POST["name"];

您可以像这样访问单个值: echo $_POST["name"];

This, of course, assumes your form is using the typical form encoding (i.e. enctype=”multipart/form-data”

当然,这假设您的表单使用典型的表单编码(即 enctype=”multipart/form-data”

If your post data is in another format (e.g. JSON or XML, you can do something like this:

如果您的帖子数据采用另一种格式(例如 JSON 或 XML,您可以执行以下操作:

$post = file_get_contents('php://input');

and $postwill contain the raw data.

并且$post将包含原始数据。

Assuming you're using the standard $_POSTvariable, you can test if a checkbox is checked like this:

假设您使用的是标准$_POST变量,您可以测试复选框是否被选中,如下所示:

if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
     ...
}

If you have an array of checkboxes (e.g.

如果您有一组复选框(例如

<form action="myscript.php" method="post">
  <input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
  <input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
  <input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
  <input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
  <input type="checkbox" name="myCheckbox[]" value="E" />val5
  <input type="submit" name="Submit" value="Submit" />
</form>

Using [ ]in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox']won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.

使用[ ]在复选框名称表示所选择的值将通过PHP脚本作为阵列进行访问。在这种情况下,$_POST['myCheckbox']不会返回单个字符串,而是返回一个数组,该数组由选中的复选框的所有值组成。

For instance, if I checked all the boxes, $_POST['myCheckbox']would be an array consisting of: {A, B, C, D, E}. Here's an example of how to retrieve the array of values and display them:

例如,如果我选中所有框,$_POST['myCheckbox']将是一个包含以下内容的数组:{A, B, C, D, E}。以下是如何检索值数组并显示它们的示例:

  $myboxes = $_POST['myCheckbox'];
  if(empty($myboxes))
  {
    echo("You didn't select any boxes.");
  }
  else
  {
    $i = count($myboxes);
    echo("You selected $i box(es): <br>");
    for($j = 0; $j < $i; $j++)
    {
      echo $myboxes[$j] . "<br>";
    }
  }

回答by Tudor Constantin

you should be able to access them from $_POSTvariable:

您应该能够从$_POST变量访问它们:

foreach ($_POST as $param_name => $param_val) {
    echo "Param: $param_name; Value: $param_val<br />\n";
}

回答by mtizziani

It is deprecated and not wished to access superglobals directly (since php 5.5 i think?)

它已被弃用,并且不希望直接访问超全局变量(我认为是 php 5.5 起?)

Every modern IDE will tell you:

每个现代 IDE 都会告诉您:

Do not Access Superglobals directly. Use some filter functions (e.g. filter_input)

不要直接访问超全局变量。使用一些过滤功能(例如filter_input

For our solution, to get all request parameter, we have to use the method filter_input_array

对于我们的解决方案,要获取所有请求参数,我们必须使用该方法 filter_input_array

To get all params from a input method use this:

要从输入法中获取所有参数,请使用以下命令:

$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...

Now you can use it in var_dumpor your foreach-Loops

现在你可以在var_dump你的foreach-Loops 中使用它

What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULLand that is correct.

不起作用的是使用此方法访问 $_REQUEST Superglobal。它总是返回NULL,这是正确的。

If you need to get all Input params, comming over different methods, just merge them like in the following method:

如果您需要获取所有输入参数,通过不同的方法,只需像以下方法一样合并它们:

function askForPostAndGetParams(){
    return array_merge ( 
        filter_input_array(INPUT_POST), 
        filter_input_array(INPUT_GET) 
    );
}

Edit: extended Version of this method (works also when one of the request methods are not set):

编辑:此方法的扩展版本(在未设置其中一种请求方法时也有效):

function askForRequestedArguments(){
    $getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
    $postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
    $allRequests = array_merge($getArray, $postArray);
    return $allRequests;
}

回答by mario

So, something like the $_POSTarray?

那么,像$_POST数组这样的东西?

You can use http_build_query($_POST)to get them in a var=xxx&var2=yyystring again. Or just print_r($_POST)to see what's there.

您可以使用http_build_query($_POST)再次将它们放入var=xxx&var2=yyy字符串中。或者只是print_r($_POST)为了看看那里有什么。

回答by bubbahut

Why not this, it's easy:

为什么不这样做,这很容易:

extract($_POST);

回答by bubbahut

Using this u can get all post variable

使用这个你可以得到所有的帖子变量

print_r($_POST)