-
Notifications
You must be signed in to change notification settings - Fork 25
/
display_errors.html
125 lines (106 loc) · 3.67 KB
/
display_errors.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
layout: default
navPage: docs
heading: Displaying the Errors
breadcrumbs:
- Tutorials,/tutorials/
- PHP Validation,/tutorials/php_validation/
- Displaying the Errors
prev: Adding Validation,/tutorials/php_validation/adding_validation/
next: Further Information,/tutorials/php_validation/further_information/
categories: tutorial
tags: validation
---
{% include open_section.html nav='nav_tutorial_php_validation.html' selected='display_errors' nav_width=4 %}
<p>
Almost there! The final step is to display the errors to the user. At this juncture, the
errors are stored in the <b>$errors</b> PHP variable. So all we need to do is loop
through them and display them on the page. Here's some code for that:
</p>
{% codemirror php %}
<?php
// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user
if (!empty($errors))
{
echo "<div class='error'>Please fix the following errors:\n<ul>";
foreach ($errors as $error)
echo "<li>$error</li>\n";
echo "</ul></div>";
}
?>
{% endcodemirror %}
<p>
Now all we do is add thank chunk of code to your form and we're done!
</p>
{% codemirror php %}
<?php
require_once("path/to/form_tools/global/api/api.php");
$fields = ft_api_init_form_page("X"); // X would be your form ID
// validation time!
$errors = array();
if (isset($_POST['sbmt']))
{
$rules = array();
$rules[] = "required,subject,Please select a subject.";
$rules[] = "required,fullname,Please enter your name.";
$rules[] = "required,email,Please enter your email address.";
$rules[] = "valid_email,email,Please enter a valid email address.";
$rules[] = "required,comments,Please enter your comments.";
$errors = validate_fields($_POST, $rules);
// no errors - great! Now we process the page. The ft_api_process_form does
// the job of both updating the database and redirecting to the next page
if (empty($errors))
{
$params = array(
"submit_button" => "sbmt",
"next_page" => "thanks.php",
"form_data" => $_POST,
"finalize" => true
);
ft_api_process_form($params);
}
// it failed validation. Update $fields with the latest contents of the form data
else
{
$fields = array_merge($_SESSION["form_tools_form"], $_POST);
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<?php
// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user
if (!empty($errors))
{
echo "<div class='error'>Please fix the following errors:\n<ul>";
foreach ($errors as $error)
echo "<li>$error</li>\n";
echo "</ul></div>";
}
?>
<form method="post" action="<?=$_SERVER["PHP_SELF"]?>">
Subject: <select name="subject">
<option value=""
<?php if (@$fields["subject"] == "") echo "selected"; ?>>Please Select</option>
<option value="General Comment"
<?php if (@$fields["subject"] == "General Comment") echo "selected"; ?>>General Comment</option>
<option value="Bug Report"
<?php if (@$fields["subject"] == "Bug Report") echo "selected"; ?>>Bug Report</option>
<option value="Feature Suggestion"
<?php if (@$fields["subject"] == "Feature Suggestion") echo "selected"; ?>>Feature Suggestion</option>
</select><br />
Name: <input type="text" name="fullname" value="<?=htmlspecialchars(@$fields["fullname"])?>" /><br />
Email: <input type="text" name="email" value="<?=htmlspecialchars(@$fields["email"])?>" /><br />
Comments: <textarea name="comments"><?=@$comments?></textarea>
<input type="submit" name="sbmt" value="Submit Form" />
</form>
</body>
</html>
{% endcodemirror %}
{% include close_section.html %}