Php get all post variables

$_POST

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

Examples

Example #1 $_POST example

Assuming the user POSTed name=Hannes

The above example will output something similar to:

Notes

Note:

This is a ‘superglobal’, or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

See Also

User Contributed Notes 6 notes

One feature of PHP’s processing of POST and GET variables is that it automatically decodes indexed form variable names.

I’ve seem innumerable projects that jump through extra & un-needed processing hoops to decode variables when PHP does it all for you:

With the first example you’d have to do string parsing / regexes to get the correct values out so they can be married with other data in your app. whereas with the second example.. you will end up with something like:
var_dump ( $_POST [ ‘person’ ]);
//will get you something like:
array (
0 => array( ‘first_name’ => ‘john’ , ‘last_name’ => ‘smith’ ),
1 => array( ‘first_name’ => ‘jane’ , ‘last_name’ => ‘jones’ ),
)
?>

This is invaluable when you want to link various posted form data to other hashes on the server side, when you need to store posted data in separate «compartment» arrays or when you want to link your POSTed data into different record handlers in various Frameworks.

Remember also that using [] as in index will cause a sequential numeric array to be created once the data is posted, so sometimes it’s better to define your indexes explicitly.

I know it’s a pretty basic thing but I had issues trying to access the $_POST variable on a form submission from my HTML page. It took me ages to work out and I couldn’t find the help I needed in google. Hence this post.

Make sure your input items have the NAME attribute. The id attribute is not enough! The name attribute on your input controls is what $_POST uses to index the data and therefore show the results.

If you want to receive application/json post data in your script you can not use $_POST. $_POST does only handle form data.
Read from php://input instead. You can use fopen or file_get_contents.

// Get the JSON contents
$json = file_get_contents ( ‘php://input’ );

// decode the json data
$data = json_decode ( $json );
?>

There’s an earlier note here about correctly referencing elements in $_POST which is accurate. $_POST is an associative array indexed by form element NAMES, not IDs. One way to think of it is like this: element «id=» is for CSS, while element «name text» name=»txtForm»>.

Note that $_POST is NOT set for all HTTP POST operations, but only for specific types of POST operations. I have not been able to find documentation, but here’s what I’ve found so far.

In other words, for standard web forms.

A type used for a generic HTTP POST operation.

For a page with multiple forms here is one way of processing the different POST values that you may receive. This code is good for when you have distinct forms on a page. Adding another form only requires an extra entry in the array and switch statements.

if (!empty( $_POST ))
// Array of post values for each different form on your page.
$postNameArr = array( ‘F1_Submit’ , ‘F2_Submit’ , ‘F3_Submit’ );

// Find all of the post identifiers within $_POST
$postIdentifierArr = array();

foreach ( $postNameArr as $postName )
if ( array_key_exists ( $postName , $_POST ))
$postIdentifierArr [] = $postName ;
>
>

// Only one form should be submitted at a time so we should have one
// post identifier. The die statements here are pretty harsh you may consider
// a warning rather than this.
if ( count ( $postIdentifierArr ) != 1 )
count ( $postIdentifierArr ) < 1 or
die( «\$_POST contained more than one post identifier: » .
implode ( » » , $postIdentifierArr ));

// We have not died yet so we must have less than one.
die( «\$_POST did not contain a known post identifier.» );
>

switch ( $postIdentifierArr [ 0 ])
case ‘F1_Submit’ :
echo «Perform actual code for F1_Submit.» ;
break;

case ‘Modify’ :
echo «Perform actual code for F2_Submit.» ;
break;

case ‘Delete’ :
echo «Perform actual code for F3_Submit.» ;
break;
>
>
else // $_POST is empty.
echo «Perform code for page without POST data. » ;
>
?>

Источник

[php] Get all variables sent with POST?

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:

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?

This question is related to php http-post

The answer is

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.

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: . 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): 
"); for($j = 0; $j < $i; $j++) < echo $myboxes[$j] . "
"; > >

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

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

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()

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

function askForRequestedArguments()

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.

Using this u can get all post variable

Источник

How to get POST array value in PHP

This article will introduce the basic usage of PHP’s post array to get a value from an input form. The article will also highlight some important points about how to use post arrays, what they are used for, and where you can find more information about them.

PHP provides an array data type for storing collections of values. The POST variable is a form input that gets sent to your PHP code by the web browser, and it allows you to store multiple values passed in from the HTML form.

An Introduction To POST Arrays in PHP.

A POST array stores data that was sent to the server by a form.

Both these methods place limitations on what can be sent to the server, GET requests are limited to 8-bit ASCII and browsers may prevent more than 2kb of data from being submitted in a single transaction.

POST arrays can store extremely large amounts of data without any limitations. Imagine you have an HTML form that asks people for their entire resume, in this case, it would make sense to save the data submitted by the form to a POST array.

How are POST arrays created?

Creating a new PHP post array is different than creating a normal array because you must tell PHP which special variable name it should use to store the post data.

There are many special variables that PHP uses, but we’re only concerned with $_POST . $_POST contains all values submitted via the HTTP POST method. $_REQUEST – Contains all values submitted via any method (GET, POST, COOKIE, etc).

As mentioned earlier GET requests can’t send very much data at once and as such, they shouldn’t be used for sending large amounts of information (like user uploads) or large forms (like user resumes).

How to get POST array value in PHP

To get all post data in PHP, we have to use the $_POST superglobal variable. You can retrieve the value of a specific key using $_POST[‘key’] syntax but if the key does not exist then this returns an empty string.

If you want to check that a given key exists or not, use the isset() function, for example:

If you want to know whether the given POST array has any content, use empty() function, for example:

Retrieve post array values

If you want to pass an array into a post variable you can do this like below-

 Cooking  
Singing
Playing
Swimming

We now want to see which hobby the user has selected.

Источник

Читайте также:  Html style fixed width
Оцените статью