Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print $_POST variable name along with value

Tags:

I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.

I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)

foreach ($_POST as $entry) {      print $entry . "<br>"; } 

Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)

like image 230
Joseph U. Avatar asked Aug 15 '10 21:08

Joseph U.


People also ask

What is $_ POST []?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".

How do you print an array POST?

To print POST data array: echo print_r($_POST,true); If you want better legibility within a browser, wrap in <pre> tags.

When you use $_ POST variable to collect data the data is visible to?

Correct Option: A. The POST method, data is invisible to others.

Is $_ POST an array?

The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.


1 Answers

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.

like image 141
Michael Robinson Avatar answered Oct 08 '22 03:10

Michael Robinson