PHP forms - required and optional fieldsMaking form fields required in PHP
PHP form
 Home   Forms tutorial   How to articles   Link to us   Donations   Contact 

PHP form > PHP forms tutorial > Required and optional fields

<< Validating forms with PHP Validating URL and E-mail >>

Required and optional fields

So far we only worked with optional fields - in all previous examples the scripts worked fine if you didn't enter any data. However, many times you want to make input fields required.

This is an easy task, let's edit the check_input function from the previous page to read:

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

We've added an extra parameter to the form: $problem. By default $problem is empty, but if you pass a value for $problem to the function and the length of entered data is 0 the script will stop executing (die) displaying the text passed as $problem parameter.

It's actually easier than it sounds. To validate data from field "yourname" we used this so far:

$yourname = check_input($_POST['yourname']);

It still works. But if you want to make "yourname" required you now simply add ,"Error message" to the function call, like this:

$yourname = check_input($_POST['yourname'],"Enter your name!");

Now if the "yourname" fields is empty when the form is submitted, the script will stop and display "Enter your name!" text.

As easy as that, for optional fields use

$test = check_input($_POST['test']);

and for required fields add a comma and an error message before ):

$test = check_input($_POST['test'], "My error message");

Here is the final code of our myform.php script where "Your name", and "Comments" fields are required, but "Your e-mail" field is not. If the name is missing you will get an error saying "Enter your name" and if the comments are missing you will get "Write your comments" error message.

<?php
$yourname = check_input($_POST['yourname'], "Enter your name");
$email    = check_input($_POST['email']);
$likeit   = check_input($_POST['likeit']);
$comments = check_input($_POST['comments'], "Write your comments");
?>
<html>
<body>
Your name is: <?php echo $yourname; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
Do you like this website? <?php echo $likeit; ?><br />
<br />
Comments:<br />
<?php echo $comments; ?>
</body>
</html>

<?php
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        die($problem);
    }
    return $data;
}
?>

The die() PHP function just displays the error text. If you want a more fancy error page that fits your website design we can add a custom error reporting function. In this example we will name it show_error:

<?php
$yourname = check_input($_POST['yourname'], "Enter your name");
$email    = check_input($_POST['email']);
$likeit   = check_input($_POST['likeit']);
$comments = check_input($_POST['comments'], "Write your comments");
?>
<html>
<body>
Your name is: <?php echo $yourname; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
Do you like this website? <?php echo $likeit; ?><br />
<br />
Comments:<br />
<?php echo $comments; ?>
</body>
</html>

<?php
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

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

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

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

Now you can simply edit the HTML code inside show_error function as much as you like and place this PHP code where you want the error message to appear:

<?php echo $myError; ?>

Note that we printed $myError directly without passing it through check_input function. Why? Because this variable was declared inside the script so we can control exactly what it is set to and it is not possible to change it from outside the script (using either POST or GET parameters) and insert any malicious code.

We end the show_error function with exit(); which tells PHP to stop executing code after displaying the error.

 

<< Validating forms with PHP Validating URL and E-mail >>

Jump to:  
  1.1 PHP forms tutorial
  1.2 Validating forms with PHP
  1.3 Required and optional fields
  1.4 Validating URL and E-mail
  1.5 Form to mail
  1.6 Putting it all together
  1.7 Final words and further reading

Help desk software Hesk

» Copyright notice

© 2008-2024 myPHPform.com. All rights reserved. Copying or redistributing any part of this website without our written permission is expressly forbidden!

Page copy protected against web site content infringement by Copyscape

 


Help desk software

Help myPHPform by Donating!

  Home  Forms tutorial  How to articles  Link to us  Donations  Contact  
 
© Copyright PHP form 2008-2024. All rights reserved.
All trademarks are property of their respective owners.
Privacy policy