Jump to content

[SOLVED] Test All $_POST for isnumeric?


Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/69647-solved-test-all-_post-for-isnumeric/
Share on other sites

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

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

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.

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?

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.