get name of a post variable
$_POST
is populated as an associative array, so you can just do this:
foreach ($_POST as $name => $val)
{
echo htmlspecialchars($name . ': ' . $val) . "\n";
}
Additionally, if you're just interested in the field names, you can use array_keys($_POST);
to return an array of all the keys used in $_POST
. You can use those keys to reference values in $_POST
.
foreach (array_keys($_POST) as $field)
{
echo $_POST[$field];
}
foreach
documentationarray_keys
documentation
$_POST variable name is variable, how to retrieve?
Add some quotes:
$user_input = $_POST["field$id"];
How can I extract the name of a $_POST variable, not its value_
As far as I understood, you are looking for array_keys($_POST)
.
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 deal with $_POST for a variable which is set as name of a input in a form - PHP
simply write
echo "<pre>";
print_r($_POST);
exit();
and you will get array which will help you.
Print $_POST variable name along with value
If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.
Related Topics
Macd Function Returning Incorrect Values
PHP Get Height and Width in PDF File Proprieties
Replace the Last Comma with an & Sign
How to Load the Requested Language File: Language/En/Form_Validation_Lang.Php
PHP Ternary Operator Clarification
Phpdoc for Variable-Length Arrays of Arguments
How to Access MySQLi Connection in Another Class on Another Page
How to Remove Values from an Array in PHP
Destination Path for Move_Uploaded_File in PHP
Is It Acceptable to Use a Mix of Object Oriented Style with Procedural Style in Coding PHP
Zend Framework 2 - Removed Form Element Causes Validation to Fail
How to Safely Store a Password Inside PHP Code
Get Request from PHP Using File_Get_Contents with Parameters
PHP Domdocument - Get HTML Source of Body
Implementing Oauth2 Login, Fatal Error: Class 'Google_Service' Not Found
How to Detect If a User Uploaded a File Larger Than Post_Max_Size