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))
{
开发者_如何学C 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?
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>";
}
}
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...
}
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.
精彩评论