phpmo Posted September 17, 2007 Share Posted September 17, 2007 Is there a way to test all $_POST variables for being isnumeric. The script I'm writing only has numbers involved and the variables will differ as they are dynamically created using arrays from user generated forms. This is what I"m using that doesn't seem to be working. Even though all $_POST variables are numbers it still catches. if (!is_numeric($_POST)){ $emessage = "You May Only Use Numbers!<br>"; $error = 1; } Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/ Share on other sites More sharing options...
wildteen88 Posted September 17, 2007 Share Posted September 17, 2007 You'll need to loop through the post array in order to see if each item within the post array contains a number. foreach($_POST as $form_field => $user_input) { if(!is_numeric($_POST[$form_field])) { echo 'Form fields must only contain numbers!'; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/#findComment-349947 Share on other sites More sharing options...
scottybwoy Posted September 17, 2007 Share Posted September 17, 2007 Reason being is that $_POST will be an array Good luck Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/#findComment-349950 Share on other sites More sharing options...
phpmo Posted September 17, 2007 Author Share Posted September 17, 2007 I've tried this and when echoing the $_POST I'm still getting array instead of the value. foreach($_POST as $form_field => $user_input) { if(!is_numeric($_POST[$form_field])) { echo $_POST[$form_field]; $emessage = "You May Only Use Numbers!<br>"; $error = 1; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/#findComment-349961 Share on other sites More sharing options...
wildteen88 Posted September 17, 2007 Share Posted September 17, 2007 Ok, how is your form laid out. Post some example code for form here. Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/#findComment-349963 Share on other sites More sharing options...
phpmo Posted September 17, 2007 Author Share Posted September 17, 2007 Here is where the $_POST is coming from and these rows can change. The user can either select to generate 1-100 rows from this line. print "<tr><td>$variable </td><td><input type=text size=2 maxlength=2 name='a[$x]' value=$_POST[a][$x]> <input type=text size=2 maxlength=2 name='b[$x]'> <input type=text size=2 maxlength=2 name='c[$x]'> <input type=text size=2 maxlength=2 name='d[$x]'> <input type=text size=2 maxlength=2 name='e[$x]'></td></tr>"; So each name is an array so I use $_POST[b][$x] to check the value etc. Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/#findComment-349965 Share on other sites More sharing options...
wildteen88 Posted September 17, 2007 Share Posted September 17, 2007 Try the following example code: <?php if(isset($_POST['submit'])) { $fr = range('a', 'e'); foreach($fr as $f) { foreach($_POST[$f] as $k => $v) { $field = trim($_POST[$f][$k]); if(!empty($field) && !is_numeric($field)) { $error = "numbers only"; break; } } if(isset($error)) break; } if(isset($error)) echo $error; } ?> <form method="post"> <?php for($x = 0; $x < 3; $x++) { $i = $x+1; echo @<<<EOF <p> <b>Line {$i}:</b><br /> <input type="text" size="2" maxlength="2" name="a[$x]" value="{$_POST['a'][$x]}"> <input type="text" size="2" maxlength="2" name="b[$x]" value="{$_POST['b'][$x]}"> <input type="text" size="2" maxlength="2" name="c[$x]" value="{$_POST['c'][$x]}"> <input type="text" size="2" maxlength="2" name="d[$x]" value="{$_POST['d'][$x]}"> <input type="text" size="2" maxlength="2" name="e[$x]" value="{$_POST['e'][$x]}"> </p> EOF; } ?> <input type="submit" name="submit" /> </form> Is that how your form is laid out and does the form checking work fine for you? Quote Link to comment https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/#findComment-349992 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.