Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="Main">
2
 
3
<h1>Creating Contact Page</h1>
4
 
5
<p>
6
We have created a default page <tt>Home.page</tt> using the <a href="http://www.pradosoft.com/demos/quickstart/?page=GettingStarted.CommandLine">PRADO command line tool</a>. The page is relatively static because it does not containt dynamic content. In this section, we will create an interactive page named <tt>Contact</tt>.
7
</p>
8
 
9
<p>
10
The purpose of the <tt>Contact</tt> page is to collect feedback from Web users of our blog system. To achieve this goal, we plan to present users a feedback form to fill with. In this form, we will require users to provide their name, email address and feedback content. After the form is filled and submitted, an email containing the feedback will be sent to the site administrator.
11
</p>
12
 
13
<p>
14
To create the <tt>Contact</tt> page, we need two files under the <tt>pages</tt> directory: the page template file <tt>Contact.page</tt> and the page class file <tt>Contact.php</tt>.
15
</p>
16
 
17
<img src="<%~ directories2.gif %>" class="output" />
18
 
19
<com:InfoBox>
20
A <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Pages">page</a> must have either a <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.Templates1">template</a> file (extension <tt>.page</tt>) or a class file, or both:
21
</p>
22
 
23
<ul>
24
<li>A template-only page is usually a page with static content, like the homepage we already created;</li>
25
<li>A class-only page produces content purely based on the execution of the class methods;</li>
26
<li>A page with both template and class combines the advantage of both: it uses template to easily organize the page layout and it uses class to contain necessary logic producing dynamic content.</li>
27
</ul>
28
</com:InfoBox>
29
 
30
 
31
<h2>Creating Page Template</h2>
32
 
33
<p>
34
We first create the template file for the <tt>Contact</tt> page.
35
</p>
36
 
37
<p>
38
We use template to organize the presentational layout of the feedback form. In the template, we use <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.TextBox">textboxes</a> to collect user's name, email and feedback. And we use <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.Validation">validators</a> to ensure that the user provides all these information before submitting the feedback form. The whole template is as follows,
39
</p>
40
 
41
<com:TTextHighlighter CssClass="source" Language="prado">
42
<html>
43
<head><title>My Blog - Contact</title></head>
44
<body>
45
<h1>Contact</h1>
46
<p>Please fill out the following form to let me know your feedback on my blog. Thanks!</p>
47
 
48
&lt;com:TForm>
49
 
50
<span>Your Name:</span>
51
&lt;com:TRequiredFieldValidator ControlToValidate="Name"
52
    ErrorMessage="Please provide your name."
53
    Display="Dynamic" />
54
<br/>
55
&lt;com:TTextBox ID="Name" />
56
 
57
<br/>
58
<span>Your Email:</span>
59
&lt;com:TRequiredFieldValidator ControlToValidate="Email"
60
    ErrorMessage="Please provide your email address."
61
    Display="Dynamic" />
62
&lt;com:TEmailAddressValidator ControlToValidate="Email"
63
    ErrorMessage="You entered an invalid email address."
64
    Display="Dynamic" />
65
<br/>
66
&lt;com:TTextBox ID="Email" />
67
 
68
<br/>
69
<span>Feedback:</span>
70
&lt;com:TRequiredFieldValidator ControlToValidate="Feedback"
71
    ErrorMessage="Please provide your feedback."
72
    Display="Dynamic" />
73
<br/>
74
&lt;com:TTextBox ID="Feedback"
75
    TextMode="MultiLine"
76
    Rows="10"
77
    Columns="40" />
78
 
79
<br/>
80
&lt;com:TButton Text="Submit" OnClick="submitButtonClicked" />
81
 
82
&lt;/com:TForm>
83
 
84
</body>
85
</html>
86
</com:TTextHighlighter>
87
 
88
<p>
89
As we can see that the template looks very similar to a normal HTML page. The main difference is that the template contains a few <tt>&lt;com:&gt;</tt> tags. Each <tt>&lt;com:&gt;</tt> tag refers to a <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Controls">control</a> whose properties are being initialized with name-value pairs in the tag. For example, the <tt>&lt;com:TButton&gt;</tt> refers to the <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.Button">TButton</a> control which displays a button that users can click on to submit the feedback form. For complete template syntax, please refer to the <a href="http://www.pradosoft.com/demos/quickstart/?page=Configurations.Templates1">Quickstart Tutorial</a>.
90
</p>
91
 
92
<com:InfoBox>
93
PRADO provides a control for every type of HTML input. For example, <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.TextBox">TTextBox</a> displays a text input field, <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.List">TDropDownList</a> displays a combobox. Each control is a component that may be accessed in code as an object with configurable properties.
94
</com:InfoBox>
95
 
96
<p>
97
Besides <tt>TTextBox</tt> controls, the template also uses many validator controls which ensure user's inputs satisfy specific validation rules. For example, to ensure a legitimate email address is provided, we use two validators to validate the "email" text box, as shown in the following:
98
</p>
99
 
100
<com:TTextHighlighter CssClass="source" Language="prado">
101
<span>Your Email:</span>
102
&lt;com:TRequiredFieldValidator
103
    ControlToValidate="Email"
104
    ErrorMessage="Please provide your email address."
105
    Display="Dynamic" />
106
&lt;com:TEmailAddressValidator
107
    ControlToValidate="Email"
108
    ErrorMessage="You entered an invalid email address."
109
    Display="Dynamic" />
110
<br/>
111
&lt;com:TTextBox ID="Email" />
112
<br/>
113
</com:TTextHighlighter>
114
 
115
<p>
116
Below we summarize the controls that are used in the page template:
117
</p>
118
 
119
<ul>
120
<li><a href="http://www.pradosoft.com/docs/classdoc/TForm">TForm</a> displays an HTML form. Any input control must be enclosed within it. And most importantly, at most one <tt>TForm</tt> may appear in a page.</li>
121
<li><a href="http://www.pradosoft.com/docs/classdoc/TTextBox">TTextBox</a> displays a text box to collect user text input.</li>
122
<li><a href="http://www.pradosoft.com/docs/classdoc/TRequiredFieldValidator">TRequiredFieldValidator</a> ensures that the associated text box is not empty when the feedback is submitted.</li>
123
<li><a href="http://www.pradosoft.com/docs/classdoc/TEmailAddressValidator">TEmailAddressValidator</a> ensures that the textbox contains a <i>valid</i> email address when the feedback is submitted.</li>
124
</ul>
125
 
126
<com:TipBox>
127
Writing templates with plain text editors could be tedious and not intuitive for designers. To ease this situation, PRADO has included in the releases an Adobe Dreamweaver extension that supports auto-completing PRADO tags (e.g. including control names, property names, event names, etc.) in Dreamweaver.
128
</com:TipBox>
129
 
130
 
131
<h2>Creating Page Class</h2>
132
 
133
<p>
134
We now create the page class <tt>Contact.php</tt>. The reason we need a page class is because we need to respond to the feedback that the user submits.
135
 
136
<p>
137
Notice in the template we have the following line. The template essentially states that when a user clicks on the button, it should call the <tt>submitButtonClicked()</tt> method. Here <tt>OnClick</tt> is the name of the user click event, and the method must be defined in the page class.
138
</p>
139
 
140
<com:TTextHighlighter CssClass="source" Language="prado">
141
  &lt;com:TButton Text="Submit" OnClick="submitButtonClicked" />
142
</com:TTextHighlighter>
143
 
144
<p>
145
We thus write down the page class as follows:
146
</p>
147
 
148
<com:TTextHighlighter CssClass="source">
149
<?php
150
class Contact extends TPage
151
{
152
    /**
153
     * Event handler for the OnClick event of the submit button.
154
     * @param TButton the button triggering the event
155
     * @param TEventParameter event parameter (null here)
156
     */
157
    public function submitButtonClicked($sender, $param)
158
    {
159
        if ($this->IsValid)  // check if input validation is successful
160
        {
161
            // obtain the user name, email, feedback from the textboxes
162
            $name = $this->Name->Text;
163
            $email = $this->Email->Text;
164
            $feedback = $this->Feedback->Text;
165
 
166
            // send an email to administrator with the above information
167
            $this->mailFeedback($name, $email, $feedback);
168
        }
169
    }
170
 
171
    protected function mailFeedback($name, $email, $feedback)
172
    {
173
        // implementation of sending the feedback email
174
    }
175
}
176
?>
177
</com:TTextHighlighter>
178
 
179
<p>
180
The above code is largely self-explanatory. In fact, we just show the event-driven programming scheme. In the event handler <tt>submitButtonClicked()</tt>, we retrieve the user's input. For example, <tt>$this->Name->Text</tt> returns the <tt>Text</tt> property value of the <tt>Name</tt> control which is the textbox collecting user's name information.
181
</p>
182
 
183
<com:InfoBox>
184
Page class name must be the same as the file name. This is also a requirement for writing any PRADO component class.
185
</com:InfoBox>
186
 
187
 
188
<h2>Testing</h2>
189
 
190
<p>
191
Our newly created <tt>Contact</tt> can be tested via the URL <tt>http://hostname/blog/index.php?page=Contact</tt>. If we click on the submit button without entering any information, we will see error messages appearing next to the corresponding textboxes. If we enter all required information, the method <tt>mailFeedback()</tt> will be invoked.
192
</p>
193
 
194
<img src="<%~ output.gif %>" class="output" />
195
 
196
<p>
197
A further enhancement to this page is to show some confirmation message on the page after the user submits feedback. And possibly, the browser may be redirected to another page if the submission is successful. We will leave these tasks to our readers.
198
</p>
199
 
200
<com:InfoBox>
201
Each validator represents a validation rule. A single input control can be associated with one or multiple validators. Validators perform validation on both client side and server side. On the client side, namely the browser, validation is done using javascript; on the server side, validation is done using PHP code. Client-side validation can be turned off, while server-side validation cannot. This ensures user inputs are always checked by the specified validation rules.
202
</com:InfoBox>
203
 
204
</com:TContent>