1

I have an HTML form that looks like this:

<form action="index.php" method="post">
<input type="hidden" value="Hidden Value" name="A Hidden Value" />
<select name="dropdownOption">
  <option value="First Choice +200">First Choice</option>
  <option value="Second Choice +300">Second Choice</option>
</select>
<p><input type="checkbox" value="Rocket +100"> Rocket</p>
</form>

I'm looping through the values of this form like this:

 foreach($_POST as $key => $value) {

 }

How would I exclude the hidden inputs from the foreach loop?

On a side note, does all versions of PHP support spaces in the input name (e.g. is valid?) In my version of PHP it automatically replaces spaces with underscores which is great, does it do that for all versions of PHP?

Thanks for any help.

4
  • 1
    You shouldn't put spaces in identifiers. It's bad practice, and some PHP installs might break with them included. If you want to ignore hidden inputs, why are they in your form in the first place? If you're doing things with JavaScript, use data-* attributes instead. Commented Mar 20, 2012 at 1:00
  • It's not with Javascript it's PHP, the hidden attributes are used for another purpose outside of the foreach loop. I need to target everything that isn't a hidden input in the foreach loop. Commented Mar 20, 2012 at 1:01
  • You can't distinguish from server side the posted field type, and I agree with JamWaffles, it's a very bad practice what you are trying to do now. Commented Mar 20, 2012 at 1:03
  • In your PHP, you could make an array containing the names of fields you want to ignore, then do some sort of boolean operation on $_POST using PHP's array functions. Otherwise, make an array with the fields you want to loop through, then do a foreach(). Commented Mar 20, 2012 at 1:05

3 Answers 3

3

There is no way to distinguish between different types of HTML input fields in php. My suggestion would to be to use some sort of prefix or other identifier in the names of your hidden fields. Then you can check to see if this prefix is present in the name of your fields in the loop. You could do

if (strpos($key, 'hdn_') === false) // proceed 
Sign up to request clarification or add additional context in comments.

2 Comments

You're going to want to make that if (strpos($key,'hdn_') === false). The identifier at the beginning of the string will return position 0, which will evaluate to false ...
You're right. I thought about that after I posted it but I was already driving. Thanks for the correction.
1
  1. Avoid using spaces within identifiers.
  2. If you just want to "filter" the hidden fields then try this :

<?php
// the not-hidden ones
$allowed_field_names = array("some_field", "another_field"); 

// Now, we're going to 'filter' the "hidden" fields
// The rest (the "allowed" ones are stored in $newPost
$newPost = array_intersect_key($_POST, array_flip($allowed_field_names));

foreach ($newPost as $key=>$value)
{
     // Do sth
}
?>

Comments

0

There is no built in way to do what you ask. That's where "programming" comes into play.
One way to solve this would be to pass what you put in the hidden elements in the GET instead.
That is, add it to the action attribute of the form element.

Comments

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.