Jump to content

Help with the $_POST SuperGlobal


thepip3r

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!
Link to comment
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
Link to comment
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"
Link to comment
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
Link to comment
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--]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.