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
https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/
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
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
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--]
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.

Archived

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

×
×
  • 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.