97

I need to insert all variables sent with post, they were checkboxes each representing an 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?

lisovaccaro
  • 30,232
  • 96
  • 246
  • 398
  • 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

6 Answers6

186

The variable $_POST is automatically populated.

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

You can access individual values like this: echo $_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>";
    }
  }
M Rostami
  • 3,699
  • 1
  • 33
  • 38
Code Magician
  • 22,161
  • 5
  • 58
  • 77
85

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

foreach ($_POST as $param_name => $param_val) {
    echo "Param: $param_name; Value: $param_val<br />\n";
}
Noel
  • 8,638
  • 28
  • 44
  • 63
Tudor Constantin
  • 25,256
  • 7
  • 48
  • 70
14

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
  • 828
  • 10
  • 22
  • 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
10

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
  • 141,508
  • 20
  • 234
  • 284
9

Why not this, it's easy:

extract($_POST);
josliber
  • 43,000
  • 12
  • 95
  • 132
bubbahut
  • 141
  • 2
  • 6
  • 4
    Terrible idea as-is; giant security hole. – nobody Sep 13 '15 at 01:42
  • 4
    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
6

Using this u can get all post variable

print_r($_POST)