PHP | Get input name via $ _POST []

HTML example:

<form method="post" id="form" action="form_action.php"> <input name="email" type="text" /> </form> 

User fills in the input field with: dfg@dfg.com

 echo $_POST['email']; //output: dfg@dfg.com 

The name and value of each input on the form are sent to the server. Is there a way to get the name property? So something like ..

 echo $_POST['email'].name; //output: email 

EDIT: To clarify some confusion in my question;

The idea was to dynamically check each input using a switch. I use a separate validation class to keep everything clean. This is a short example of my end result:

 //Main php page if (is_validForm()) { //All input is valid, do something.. } //Separate validation class function is_validForm () { foreach ($_POST as $name => $value) { if (!is_validInput($name, $value)) return false; } return true; } function is_validInput($name, $value) { if (!$this->is_input($value)) return false; switch($name) { case email: return $this->is_email($value); break; case password: return $this->is_password($value); break; //and all other inputs } } 

Thanks raina77ow and everyone else!

+8
post properties php
source share
8 answers

You can process $ _POST in a foreach to get both names and their values, for example:

 foreach ($_POST as $name => $value) { echo $name; // email, for example echo $value; // the same as echo $_POST['email'], in this case } 

But you cannot get the property name from the value of $_POST['email'] - this is a simple string, and it does not preserve its "origin".

+15
source share
 foreach($_POST as $key => $value) { echo 'Key is: '.$key; echo 'Value is: '.$value; } 
0
source share

If you want to do it dynamically, you can do it like this:

 foreach ($_POST as $key => $value) { echo 'Name: ', $key, "\nValue: ", $value, "\n"; } 
0
source share

Scroll object/array with foreach :

 foreach($_POST as $key => $items) { echo $key . "<br />"; } 

Or you can use var_dump or print_r to debug large variables like arrays or objects :

 echo '<pre>' . print_r($_POST, true) . '</pre>'; 

or

 echo '<pre>'; var_dump($_POST); echo '</pre>'; 
0
source share

current (array_keys ($ _ POST)) should give you what you are looking for. Not tested.

0
source share

Actually, I found something that might work for you, look at this -

http://php.net/manual/en/function.key.php

This page says the code below

 <?php $array = array( 'fruit1' => 'apple', 'fruit2' => 'orange', 'fruit3' => 'grape', 'fruit4' => 'apple', 'fruit5' => 'apple'); // this cycle echoes all associative array // key where value equals "apple" while ($fruit_name = current($array)) { if ($fruit_name == 'apple') { echo key($array).'<br />'; } next($array); } ?> 

will give you the result

fruit1
fruit4
fruit5

The easiest way.

0
source share

A nice neat way to see the names of forms that will be submitted or submitted

 <?php $i=1; foreach ($_POST as $name => $value) { echo "</b><p>".$i." ".$name; echo " = <b>".$value; $i++; } ?> 

Just submit your form to this script and it will show you what is being submitted.

0
source share

You can use the foreach Loop loop to get all the values ​​that are set.

 foreach ($_POST AS $k=>$v){ echo "$k is $v"; } 

Your example

 echo $_POST['email'].name; //output: email 

doesn't make sense since you already know the name of the value you are accessing?

-2
source share

All Articles