kakaajee
kakaajee

Reputation: 227

Getting All POST Variables instead of hidden fields and submit button

I have a form where text fields are created dynamically with the names unknown. I am trying to get those text fields names and values and using the following PHP code.

foreach ($_POST as $key => $entry)
{
     if (is_array($entry))
     {
        foreach($entry as $value)
        {
           print $key . ": " . $value . "<br>";
        }
     }
    else 
     {
        print $key . ": " . $entry . "<br>";
     }
}

But the problem is that it gets all the hidden fields and submit button values as well. How can I prevent that from happening?

Upvotes: 1

Views: 1226

Answers (3)

Abel
Abel

Reputation: 57179

Essentially, a hidden field and a submit button are form elements that should (must!) be send as part of a POST request, if they are inside <form> tags. This is standard behavior. Otherwise a server would not be able to determine the value of a hidden field, or determine which submit button was pressed.

So, you'll have to make exceptions if you don't want to process these fields. A simple workaround is to use a naming-convention (i.e., hidden_ or submit_) for the fields you do not wish to process, then you simply check whether a key starts with one of those unwanted names.

Upvotes: 0

Rukmi Patel
Rukmi Patel

Reputation: 2561

unless and until you know names of the fields , you can not put constrains.

in your case one alternative solution is you can put condition in loop

like

if($key == 'btn_name')
{

   code....

}

else{

   code...

}

Upvotes: 0

genesis
genesis

Reputation: 50982

foreach ($_POST as $key => $entry)
{
     if ($key == "button_submit") continue;
     if ($key == "hidden_field") continue;
     if ($key == "hidden_field2") continue;
     if ($key == "hidden_field3") continue;
     if (is_array($entry))
     {
        foreach($entry as $value)
        {
           print $key . ": " . $value . "<br>";
        }
     }
    else 
     {
        print $key . ": " . $entry . "<br>";
     }
}

Upvotes: 1

Related Questions