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>";
}
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>";
}
}
PHP: Possible to automatically get all POSTed data And Multiple checkbox unchecked?
Only "successful" controls are submitted. An unchecked checkbox or radio button is not "successful".
You need to declare a default value with a hidden input. Make sure the hidden input comes before the checkbox so if the checkbox is checked it will override the default hidden input since the names are the same:
<input name="p-sis-0110-1" type="hidden" value="0">
<input name="p-sis-0110-1" type="checkbox" value="1">
To use an array you need to explicitly define the indexes so they are the same:
<input name="input[0]" type="hidden" value="0">
<input name="input[0]" type="checkbox" value="1">
How can I get all posted data as one single line in php?
I don't quite understand what you want, but I think you're looking for the following:
On a single line:
$raw_data = file_get_contents("php://input");
in an array
$array_data = $_POST // this is already an array?
How can I use $POST in PHP to automatically collect data from HTML5 inputs and send through e-mail?
You can try like this:
$body="";
foreach ($_POST as $key => $value) {
$body.= str_replace("_"," ",$key).": ". $value."\n";
}
provided that $key
holds the descriptive information which you need with words separated by underscore (_) as in your question is.
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 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.
How can I get all submitted form values in PHP and automatically assign them to variables?
Look at the extract function : http://www.php.net/manual/en/function.extract.php
Posted form data remains in memory
you can force to unset it ,
unset(var) OR unset($_POST['message'])
Is it possible to automatically post a client's form from server side?
When you say "posting a client's form" it does the following things:
- Initialize form submission with data in the form fields via browser API.
- Send the POST/GET request to the destination server.
The step 1 can only be done in a client side - because the data is in the browser and PHP has no part in handling the browser when the page is fully loaded.
But step 2 can be done using almost any language and make the destination server record the data via TCP sockets. PHP is just a candidate here.
What your second article does is this.
Related Topics
How to Count Days Between Two Dates in PHP
Create a Dynamic MySQL Query Using PHP Variables
Understanding PHP & (Ampersand, Bitwise And) Operator
When to Pass-By-Reference in PHP
Executing a Shell Script from a PHP Script
Post and Get at the Same Time in PHP
Retrieve All Hashtags from a Tweet in a PHP Function
Regex Pattern to Get the Youtube Video Id from Any Youtube Url
How to Handle Error for Duplicate Entries
What Causes: "Notice: Uninitialized String Offset" to Appear
Utf-8 Encoded HTML Pages Show Questions Marks Instead of Characters
PHP Date Format /Date(1365004652303-0500)/
Jquery Mobile: How to Correctly Submit Form Data