Get all variables sent using POST?

I need to insert all the variables sent with the message, they were flags, each of which represented the user.

If I use GET, I get something like this:

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

I need to insert variables into the database.

How to get all variables sent using POST? Like an array or values ​​separated by lumps or something else?

+43
Nov 21 '11 at 5:02
source share
6 answers

The variable $_POST automatically populated.

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

Access to individual values ​​can be obtained like this: echo $_POST["name"];

This, of course, assumes that your form uses the typical encoding of the form (ie enctype="multipart/form-data"

If your message data is in a different format (for example, JSON or XML, you can do something like this:

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

and $post will contain raw data.

Assuming you are using the standard $_POST variable, you can check if the checkbox is checked:

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

If you have a set of flags (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 check box indicates that the selected script will be accessed by the PHP script as an array. In this case, $_POST['myCheckbox'] will not return a single row, but will return an array consisting of all the values ​​of the checked flags.

For example, if I checked all the fields, $_POST['myCheckbox'] would be an array consisting of: {A, B, C, D, E} . Here is an example of how to get an 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): "); for($j = 0; $j < $i; $j++) { echo($myboxes[$i] . " "); } } 
+81
Nov 21 2018-11-11T00:
source share

you must have access to them from the $_POST variable:

 foreach ($_POST as $param_name => $param_val) { echo "Param: $param_name; Value: $param_val<br />\n"; } 
+54
Nov 21 '11 at 5:05
source share

So, something like an $_POST array?

You can use http_build_query($_POST) to add them to the var=xxx&var2=yyy line again. Or just print_r($_POST) to see what's there.

+5
Nov 21 2018-11-11T00:
source share

It is deprecated and does not want to directly access superglobals (since php 5.5, I think?)

Every modern IDE will tell you:

You can not directly contact the superglobal. Use some filter functions (e.g. filter_input )

For our solution, in order to get all query parameters, we must use the filter_input_array method

To get all parameters from an 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 in foreach -Loops

What doesn't work is to access $ _REQUEST Superglobal using this method. It Allways returns NULL , and rightly so.

If you need to get all the input parameters using different methods, just merge them, as in the following method:

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

Edit: An extended version of this method (also works when one of the request methods is not installed):

 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; } 
+3
Mar 22 '16 at 15:02
source share

Why not this, easy:

 extract($_POST); 
+2
Sep 13 '15 at 0:32
source share

Using this, you can get the whole post variable

 print_r($_POST) 
+2
Jan 19 '16 at 6:18
source share



All Articles