Link to home
Start Free TrialLog in
Avatar of casit
casitFlag for United States of America

asked on

go through POST but leave one item out.

I want to go through my post items(from a form submission) however I set a hidden post in the form to check to see if the form was submitted.  So my question is How can I go through an array and check to see if items are NOT SET but leave one item out($_POST['submitted'])
ASKER CERTIFIED SOLUTION
Avatar of MasonWolf
MasonWolf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of casit

ASKER

Worked like a charm!!
That was really just one method of several. You might enjoy seeing another:

foreach($_POST AS $key=>$var)
{
     if($key != 'submitted' && $var == '')
     {
         //insert whatever code you use when a variable is not set
     }
}

It's shorter code, but from the compiler's perspective it does the exact same thing.
Avatar of casit

ASKER

Well I may have been too hasty.  Here is my current code that you provided
[php]
foreach($_POST AS $key=>$var)
{
     if($key != 'submitted' && $key != 'button' && $var == '')
     {
           $output = '2';
     }
}
[/php]
and here is a var_dump($_POST)
[output]
array(12) { ["username"]=> string(6) "kellie" ["city"]=> string(0) "" ["state"]=> string(0) "" ["occupation"]=> string(0) "" ["comp_name"]=> string(0) "" ["website"]=> string(0) "" ["interests"]=> string(0) "" ["hobbies"]=> string(0) "" ["fav_books"]=> string(0) "" ["dreams"]=> string(0) "" ["submitted"]=> string(3) "yes" ["button"]=> string(6) "Submit" }
[/output]

Basically this is NOT supposed to trigger the $output.  but it is.  any ideas?
It triggers the output because all but one of the posted variables evaluate to empty strings. The code is doing exactly what it was written to do in those cases. I thought that was what you wanted. What was supposed to happen?
Avatar of casit

ASKER

Basically I want to through the output if all of the fields that were submitted are empty except for the submitted and button field as they will always have a value.  Understand?
$output = '';
foreach($_POST AS $key=>$var)
{
     if($key != 'submitted' && $key != 'button' && $var != '')
     {
           $output = '2';
           break;
     }
}

Is this what you mean? $output would only be set if at least 1 variable isn't empty.