Jump to content


Photo

Help with the $_POST SuperGlobal


  • Please log in to reply
10 replies to this topic

#1 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 03 April 2006 - 02:17 AM

Here's my code:

if ($_POST['submit']) {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    
    foreach ($_POST as $key => $val) {
        if ($key !== 'submit' || $key !== 'AttackType') {
            if (!is_numeric($val)) {
                echo "$key $val is not a number.";
                exit;
            }

        }
    }
}

And the resulting output looks like this:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Array
(
[LAV-Anti-Tank] => 0
[M2_Bradley] => 0
[M1_Abrams] => 0
[Striker] => 0
[M1A2_Abrams] => 0
[T-72_MBT] => 0
[M1A1_MBT] => 0
[Patriot_Missile] => 45
[AttackType] => Tank
[submit] => Calc
)

AttackType Tank is not a number.[/quote]

In my foreach loop, I'm trying to not evaluate the 'AttackType' and 'submit' keys but am failing miserably and i'm sure I have to be missing something simple. Anyone have any ideas? Thanx in advance!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 April 2006 - 02:56 AM

You want to use the "!=" not the "!==" in your "if" statement. The first is true if the two values being tested are not equal, the second is true only if the datatypes of the two variables are not equal.

Ken

#3 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 03 April 2006 - 03:10 AM

I still get the same results when i changed the code to:

if (($key != 'submit') || ($key != 'AttackType')) {

#4 macdumbpling

macdumbpling
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 April 2006 - 04:52 AM

In you script, you have $_POST['submit'] which is always going to be a boolean (0 or 1) therefore always numeric.

You need to change the $_POST['submit'] that equals calc to something else.

HTH and makes sense :)
Kevin

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 April 2006 - 04:57 AM

Let's take another tack and not use an "if" statement at all. Use the "switch" statement:
<?php
if ($_POST['submit']) {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    
    foreach ($_POST as $key => $val)
         switch($key) {
             case 'submit':
             case 'AttackType':
                break;  // do nothing for these
             default:    // For all other keys
                if (!is_numeric($val))
                   exit ("$key $val is not a number."); // are you sure you just want to exit here?
         }
?>

Ken

#6 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 03 April 2006 - 05:27 AM

The || in your if statement should be &&. Think about it logically:
What you really want to know is: "if the key is not equal to submit AND the key is not equal to attacktype, then echo the content"

#7 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 03 April 2006 - 05:34 AM

haha... simplified the logic...


if ($_POST['submit']) {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    
    foreach ($_POST as $key => $val) {
        if (is_numeric($val)) {

        }
    }
    
}

i still don't know what was going on but thanx for trying kenr

#8 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 03 April 2006 - 05:47 AM

And no football... that logic is totally wrong because $key and $val will only be set to a single value for every iteration of the FOREACH loop. With taht being said, my $key var will NEVER be not equal to 'submit' AND 'AttackType'...

#9 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 03 April 2006 - 01:49 PM

Well, here. I guess the only way to show you is to prove it to you.
<?php
$array = array( 'this' , 'is' , 'an' , 'array' , 'of' , 'strings' );
foreach ( $array as $key=>$val )
{
    if ( ( $key != 4 ) || ( $key != 5 ) )
        echo $key . " - The if statement returned true";
    else
        echo $key . " - The if statement returned false";
    echo "<br />";
}
?>

Output:
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]0 - The if statement returned true
1 - The if statement returned true
2 - The if statement returned true
3 - The if statement returned true
4 - The if statement returned true
5 - The if statement returned true[!--html2--][/div][!--html3--]

Now, here's my code:
<?php
$array = array( 'this' , 'is' , 'an' , 'array' , 'of' , 'strings' );
foreach ( $array as $key=>$val )
{
    if ( ( $key != 4 ) && ( $key != 5 ) )
        echo $key . " - The if statement returned true";
    else
        echo $key . " - The if statement returned false";
    echo "<br />";
}
?>

And here's the output:
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]0 - The if statement returned true
1 - The if statement returned true
2 - The if statement returned true
3 - The if statement returned true
4 - The if statement returned false
5 - The if statement returned false[!--html2--][/div][!--html3--]

#10 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 04 April 2006 - 05:02 PM

ok... i don't really understand the how that evals to true but it did work. thanx for the clarification football...

#11 djnrempel

djnrempel
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 April 2006 - 05:46 PM

An approach that I have used is to create a separate array containing the names of the array keys you want to process. eg:
$types=array ('M2_Abrams','M1_Whatever', ...);
then

foreach ($types as $val) {
dosomething ($_POST[$val]);
}

What I like about this approach is that I can add other fields of different types to the form later without worrying about having to filter them out.

Also, you can use that same array to dynamically create those form fields instead of having to type them all in to echo statements. To add a new form to your field, you just add a new value to the $types array, which just has to be available to all the functions that would use it then.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users