13

HTML example:

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

User fills input field with: [email protected]

echo $_POST['email']; //output: [email protected]

The name and value of each input within the form is send 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 about my question;

The idea was to validate each input dynamically 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 to raina77ow and everyone else!

2
  • You know that the array key is actually the name, so when you know the key why would you need to do that? Perhaps you can suggest what you're trying to do. Commented Nov 14, 2012 at 11:28
  • See my comment below for clarification :) Commented Nov 14, 2012 at 11:57

8 Answers 8

23

You can process $_POST in foreach loop to get both names and their values, like this:

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

But you're not able to fetch the name of property from $_POST['email'] value - it's a simple string, and it does not store its "origin".

Sign up to request clarification or add additional context in comments.

2 Comments

Careful about mass-assignment attacks, though.
@WaleedKhan Care to elaborate? I'm coming across that term for the first time
3
foreach($_POST as $key => $value)
{
 echo 'Key is: '.$key;
 echo 'Value is: '.$value;
}

Comments

1

If you wanted to do it dynamically though, you could do it like this:

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

Comments

1

Loop your 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>';

Comments

1

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

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

This page says that 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);
}
?>

would give you the output,

fruit1
fruit4
fruit5

As simple as that.

Comments

0

current(array_keys($_POST)) should give you what you are looking for. Haven't tested though.

Comments

0

Nice neat way to see the form names that are, or will be submitted

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

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

Comments

-2

You can use a foreach Loop to get all values that are set.

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

Your example

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

wouldnt make sense, since you already know the name of the value you are accessing?

4 Comments

$k would be the key of the array and $v would be the value of that key
Please go, read and understand the difference between double quotes and single quotes before rating other posts down. thx.
Oh that I thought you were referring to $k=>$v
@dognose, See my comment for clarification. Thanks for help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.