Get all variables sent with POST?
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>";
}
}
How to grab all variables in a post (PHP)
If you really just want to print them, you could do something like:
print_r($_POST);
Alternatively, you could interact with them individually doing something like:
foreach ($_POST as $key => $value) {
//do something
echo $key . ' has the value of ' . $value;
}
but whatever you do.. please filter the input. SQL Injection gives everyone sleepless nights.
PHP: Possible to automatically get all POSTed data?
Sure. Just walk through the $_POST
array:
foreach ($_POST as $key => $value) {
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
PHP - Capture All POST Variables
For a PayPal IPN, you should be able to use $postdata = file_get_contents('php://input');
to fetch the raw post data needed for the validation callback.
Get all $_POST variables starting with certain text
The following uses strpos() to check that the POST string begins with book_
foreach($_POST as $key => $value) {
if (strpos($key, 'book_') === 0) {
// value starts with book_
}
}
How do I print all POST results when a form is submitted?
All the values are stored in the $_POST
collection
<?php print_r($_POST); ?>
or if you want something fancier that is easier to read use a foreach loop to loop through the $_POST
collection and print the values.
<table>
<?php
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
?>
</table>
How can I grab and store multiple $_POST Variables in a for each loop?
foreach ($_POST as $i => $v) {
if (preg_match("/^Q\d+$/",$i)) {
// Here we insert value $v to database
}
}
Is there a PHP function to call all variables from a form?
You can loop over all veriables in the $_PREQUEST array:
foreach($_REQUEST as $key=>$value){
//doStuff
}
However this will include all send parameters, not only the input. Also you should not use $_REQUEST but $_POST or $_GET
Related Topics
PHP Ternary Operator Clarification
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce
Get the Auto-Generated Id After an Insert
Unable to Scrape Content from a Website
Passing a Boolean Value from Checkbox in Laravel Form
Date Function to Display All Dates Between Two Dates
How to Safely Store a Password Inside PHP Code
Can No Longer Add Registration Fields in Magento 1.4.2.0
Pdo Mssql Server - Driver Not Found
Merge Two Png Images with PHP Gd Library
I Want Get the Sum of Files Size in Folder by PHP
How to Detect If a User Uploaded a File Larger Than Post_Max_Size
PHP - Make Multi-Dimensional Associative Array from a Delimited String
Why Don't PHP and JavaScript's Timestamps Match
Is Auto-Initialization of Multi-Dimensional Hash Array Possible in Ruby, as It Is in PHP