PHP form > PHP forms tutorial >
Required and optional fields
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:
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.
» Copyright notice
© 2008-2024 myPHPform.com. All rights reserved. Copying or redistributing
any part of this website without our written permission is expressly
forbidden!
|