Jump to content

Archived

This topic is now archived and is closed to further replies.

thepip3r

Help with the $_POST SuperGlobal

Recommended Posts

Here's my code:

[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;
            }

        }
    }
}[/code]

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I still get the same results when i changed the code to:

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Let's take another tack and not use an "if" statement at all. Use the "switch" statement:
[code]<?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?
         }
?>[/code]

Ken

Share this post


Link to post
Share on other sites
Guest footballkid4
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"

Share this post


Link to post
Share on other sites
haha... simplified the logic...


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

        }
    }
    
}[/code]

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

Share this post


Link to post
Share on other sites
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'...

Share this post


Link to post
Share on other sites
Guest footballkid4
Well, here. I guess the only way to show you is to prove it to you.
[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 />";
}
?>[/code]

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:
[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 />";
}
?>[/code]

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--]

Share this post


Link to post
Share on other sites
ok... i don't really understand the how that evals to true but it did work. thanx for the clarification football...

Share this post


Link to post
Share on other sites
An approach that I have used is to create a separate array containing the names of the array keys you want to process. eg:
[code]$types=array ('M2_Abrams','M1_Whatever', ...);[/code]
then

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

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.