Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

post - PHP | Get input name through $_POST[]

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...