thepip3r Posted April 3, 2006 Share Posted April 3, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/ Share on other sites More sharing options...
kenrbnsn Posted April 3, 2006 Share Posted April 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23343 Share on other sites More sharing options...
thepip3r Posted April 3, 2006 Author Share Posted April 3, 2006 I still get the same results when i changed the code to:if (($key != 'submit') || ($key != 'AttackType')) { Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23346 Share on other sites More sharing options...
macdumbpling Posted April 3, 2006 Share Posted April 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23361 Share on other sites More sharing options...
kenrbnsn Posted April 3, 2006 Share Posted April 3, 2006 Let's take another tack and not use an "if" statement at all. Use the "switch" statement:[code]<?phpif ($_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 Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23363 Share on other sites More sharing options...
Guest footballkid4 Posted April 3, 2006 Share Posted April 3, 2006 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" Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23368 Share on other sites More sharing options...
thepip3r Posted April 3, 2006 Author Share Posted April 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23371 Share on other sites More sharing options...
thepip3r Posted April 3, 2006 Author Share Posted April 3, 2006 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'... Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23374 Share on other sites More sharing options...
Guest footballkid4 Posted April 3, 2006 Share Posted April 3, 2006 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 true1 - The if statement returned true2 - The if statement returned true3 - The if statement returned true4 - The if statement returned true5 - 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 true1 - The if statement returned true2 - The if statement returned true3 - The if statement returned true4 - The if statement returned false5 - The if statement returned false[!--html2--][/div][!--html3--] Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23467 Share on other sites More sharing options...
thepip3r Posted April 4, 2006 Author Share Posted April 4, 2006 ok... i don't really understand the how that evals to true but it did work. thanx for the clarification football... Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23852 Share on other sites More sharing options...
djnrempel Posted April 4, 2006 Share Posted April 4, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/6439-help-with-the-_post-superglobal/#findComment-23874 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.