Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 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: dfg@dfg.com

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

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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...