107

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

If I use GET I get something like this:

?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?

starball
  • 20,030
  • 7
  • 43
  • 238
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • 3
    possible duplicate of [How to grab all variables in a post (PHP)](http://stackoverflow.com/questions/3058336/how-to-grab-all-variables-in-a-post-php) – mario Nov 21 '11 at 05:09

7 Answers7

201

The variable $_POST is automatically populated.

To see the entire contents of this array, just type

var_dump($_POST);

You can access individual values like this:

$name = $_POST["name"];

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

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

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

and $post will contain the raw data.

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

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.

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:

  $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>";
    }
  }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Code Magician
  • 23,217
  • 7
  • 60
  • 77
91

you should be able to access them from $_POST variable:

foreach ($_POST as $param_name => $param_val) {
    echo "Param: ".htmlspecialchars($param_name)."; ";
    echo "Value: ".htmlspecialchars($param_val)."<br />\n";
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
15

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

Every modern IDE will tell you:

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

For our solution, to get all request parameter, we have to use the method 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_dump or your foreach-Loops

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

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;
}
mtizziani
  • 956
  • 10
  • 23
  • i get fatal error when trying to use your suggestion "$postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();". FATAL ERROR Constant expression contains invalid operations on line number 5 – luckyguy73 Dec 21 '16 at 21:54
  • @AshtonMorgan i tested it (copy paste) and it works fine. What PHP Version do u use? – mtizziani Jan 31 '17 at 11:56
11

So, something like the $_POST array?

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

mario
  • 144,265
  • 20
  • 237
  • 291
8

Why not this, it's easy:

extract($_POST);
josliber
  • 43,891
  • 12
  • 98
  • 133
bubbahut
  • 139
  • 2
  • 6
  • 6
    Terrible idea as-is; giant security hole. – nobody Sep 13 '15 at 01:42
  • 5
    This code answers his question perfectly "How do I get all variables sent with POST?", whereas the other answers do not. And the best answer gets downvoted, go figure. Security may not be an issue for his project. Are you saying that those who made php made a mistake by inventing this function, that it should never be used? – bubbahut Sep 14 '15 at 18:02
  • 2
    @bubbahut exactly. Nobody wants all post variables for production code. – cowboysaif Dec 19 '15 at 19:51
  • @bubbahut this is NOT a "his project". This is a PUBLIC ANSWER. Now with HALF A MILLION views. And to all those people you suggested adding a GAPING HOLE in their projects. – Your Common Sense Jun 26 '23 at 05:34
  • So you want the wrong answer to be posted so you can hold someone else's hand. Wow. Maybe others should answer any questions you ask FALSELY too so they can assume how you use it and protect you from yourself. This site is specifically against babysitting. If you have a problem with this code then express your issue to the PHP development team, not a Q&A about it which CORRECTLY answers a question. You assume its for a production website, assume this, assume that. Good grief! – bubbahut Jun 27 '23 at 14:52
6

Using this u can get all post variable

print_r($_POST)
0

`$data = json_decode(file_get_contents("php://input"));
$arr=get_object_vars($data); extract($arr);

or way 2

 $data1 = json_decode(file_get_contents("php://input"));
  extract($data1);`
tedi 1700
  • 9
  • 2
  • using extract on input data is a VERY, VERY bad idea. it will overwrite local variables and basically give an attacker full control of yourscript – Your Common Sense Jun 26 '23 at 05:32