Jump to content

Recommended Posts

Hello all

 

I'm currently creating a basic form it has various fields in it. One of them is age..

What i am trying to do is when someone submits their age with the form the php scripts validates it's an integer for a start and then check to see if the age is within a certain bracket like >16 but <26.

 

here is what i have done so far:

test.html

<form action="myform.php" method="post">
<p>Firstname: *** <input type="text" name="firstname" /><br />
Surname: *** 
  <input type="text" name="surname" />
  <br />
Address: 
<input type="text" name="addressone" />
<br />
Postcode: <input type="text" name="postcode" /><br />
E-mail: <input type="text" name="email" /></p>


<p>
Age: ** 
  <input type="number" name="age" /><br />
<br />
<br />
</p>


<p><input type="submit" value="Send it!"></p>
</form>

 

myform.php

<?php
$firstname = check_input($_POST['firstname'], "Enter you firstname please");
$surname = check_input($_POST['surname'], "Enter you surnname please");
$addressone = check_input($_POST['adressone']);
$postcode = check_input($_POST['postcode']);
$email = check_input($_POST['email']);
$age = check_input($_POST['vage'], "Please enter your age");

function check_input($data, $error='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($error && strlen($data) == 0) 
    {
        show_error($error);
    }
   else return $data;
}


function show_error($myError) 
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit(); 
}
?>

<html>
<body>

Your first name is: <?php echo $firstname; ?><br />
Your Surname is: <?php echo $surname; ?><br />
Your address is: <?php echo $addressone; ?><br />
Your Postcode is: <?php echo $postcode; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
<br />
Your age is: <?php echo $age; ?><br />


</body>
</html>

 

 

Later on i wish to also add a drop down field aswell to be validated that the right choice has been selected..

 

If anyone could help me or point me to a tutorial that deals with exactly this i would be most greatful.

Link to comment
https://forums.phpfreaks.com/topic/148369-php-form-numbers-validation/
Share on other sites

http://us3.php.net/is_numeric

 

For a drop down, you usually just set a default field of "Select One" or something like that and make its value something you can check for like zero, so then you can use

 

if($_POST['whatever'] == 0) {
//Drop down item not selected
} else {
//Drop down item selected so do something with it
}

<?php

$age = $_POST['age'];

if ($age > 16 && $age < 26 )
{
print "age ok";
}
else
{
print "wrong age";
}

print <<<END
<form method="POST">
age: <input type="text" name="age">
<input type="submit" name="submit" value="submit">
END;

?>

 

Thats how you would check the age is correct

strlen($data) == 0 <<< what you mean with this statement there no length?

 

explain please.

 

yeah if the string length is = to 0 then throw up an error message

 

I am very new to php and followed at pho form guide... my updated myform code is as follows:

 

<?php
$firstname = check_input($_POST['firstname'], "Enter you firstname please");
$surname = check_input($_POST['surname'], "Enter you surnname please");
$addressone = check_input($_POST['adressone']);
$postcode = check_input($_POST['postcode']);
$email = check_input($_POST['email']);
$age = check_input($_POST['age'], "Please enter your age");

if (is_numeric($age > 16 && $age < 26))
{
return $age;
}
else
{
print ("age not valid");
}

?>
<html>
<body>

Your first name is: <?php echo $firstname; ?><br />
Your Surname is: <?php echo $surname; ?><br />
Your address is: <?php echo $addressone; ?><br />
Your Postcode is: <?php echo $postcode; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
<br />
Your age is: <?php echo $age; ?><br />


</body>
</html>
<?php
function check_input($data, $error='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($error && strlen($data) == 0) 
    {
        show_error($error);
    }
   else return $data;
}


function show_error($myError) 
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit(); 
}
?>

 

i've included an if statement for the age question... but it still does not work? i think it needs to work with the check_input function i created... to bring back the custom error message if the age entered is incorrect, else return the data entered...

i am still able to enter a string into the age field and it comes back.

 

anyone please?

if (is_numeric($age > 16 && $age < 26))

that is incorrect syntax

 

if (is_numeric($age) && $age > 16 && $age < 26)

 

Hello

Thanks for that.

 

However the result still allows to bring back a string value.. i think i am doing something wrong with my check_input function?? and maybe the age check should be in there? but i'm not sure how?

 

also if i put in a correct age... i get a blank screen??

 

thanks again

You need to process the age differently

 

<?php
$age = $_POST['age'];

if ($age == "") {
print "Please enter your age"
}
elseif(is_numeric($age) && $age > 16 && $age < 26)
{
return $age;
}
else
{
print ("age not valid");
}
?>

 

as its validity is different from check_input

Hi.. well ive just basically created another function called check_age which pretty much does the same job as the check data function exept i put my if statements in there so the code now looks like:

 

<?php
$firstname = check_input($_POST['firstname'], "Enter you firstname please");
$surname = check_input($_POST['surname'], "Enter you surnname please");
$addressone = check_input($_POST['adressone']);
$postcode = check_input($_POST['postcode']);
$email = check_input($_POST['email']);
$age = check_age($_POST['age'], "Please enter your age");

?>
<html>
<body>

Your first name is: <?php echo $firstname; ?><br />
Your Surname is: <?php echo $surname; ?><br />
Your address is: <?php echo $addressone; ?><br />
Your Postcode is: <?php echo $postcode; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
<br />
Your age is: <?php echo $age; ?><br />


</body>
</html>
<?php
function check_age($age, $error='')
{
$data = htmlspecialchars($data);
if (is_numeric($age) && $age > 16 && $age < 26)
{
 show_error($error);
}
else return $age;
}

function check_input($data, $error='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($error && strlen($data) == 0) 
    {
        show_error($error);
    }
   else return $data;
}


function show_error($myError) 
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit(); 
}
?>

 

this kind of works.... but i am still able to enter a string into the age field and its gets through.... what am i doing wrong? am i on the right track?

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.