• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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'])
0
casit
Asked:
casit
  • 4
  • 3
1 Solution
 
MasonWolfCommented:
foreach($_POST AS $key=>$var)
{
     if($key=='submitted') continue;
     if($var == '')
     {
         //insert whatever code you use when a variable is not set
     }
}
0
 
casitAuthor Commented:
Worked like a charm!!
0
 
MasonWolfCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
casitAuthor Commented:
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?
0
 
MasonWolfCommented:
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?
0
 
casitAuthor Commented:
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?
0
 
MasonWolfCommented:
$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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now