The Number Game - Adding It All Up
(Page 7 of 7 )
The final item on today's menu is perhaps the simplest - a form which allows administrators to easily add new questions to the system.
This DTML method, aptly named "addPoll", is divided into two sections, as seen in the listing below:
<dtml-var standard_html_header>
<h3>Add New Poll</h3>
<dtml-if submit>
<dtml-call
insertPoll>
<h4>Poll added successfully!</h4>
<p>
Click here to <a
href="viewCurrentPoll">view</a> it.
<p>
<dtml-else>
<table border="0"
cellspacing="5" cellpadding="5">
<form action="addPoll" method="post">
<tr>
<td>Poll
question<br><input type="Text" name="question" size="25"></td>
</tr>
<tr>
<td>Response
1<br><input type="Text" name="response1" size="25"></td>
</tr>
<tr>
<td>Response
2<br><input type="Text" name="response2" size="25"></td>
</tr>
<tr>
<td>Response
3<br><input type="Text" name="response3" size="25"></td>
</tr>
<tr>
<td
align=center><input type=submit name=submit value="Add Poll"></td>
</tr>
</form>
</table>
</dtml-if>
<dtml-var
standard_html_footer>
The first task is to check if the form has been submitted. If it has not yet been submitted, then the method generates an empty form, with fields for the question and possible responses. Once the form is submitted, the data entered into the form by the administrator is inserted into the database via the "insertPoll" Z SQL Method below.
INSERT INTO poll (question, response1, response2, response3, date)
VALUES (<dtml-sqlvar question type="string">, <dtml-sqlvar response1 type="string">, <dtml-sqlvar response2 type="string">, <dtml-sqlvar response3 type="string">, <dtml-sqlvar ZopeTime type="string">)
Note that the built-in ZopeTime DTML variable (which returns the current system time) has been used to insert the current date as part of the table record. This is similar to the now() function that is built into the MySQL database.
Here's what the form and its result look like:
And that's about it. Hopefully, this exercise gave you some insight into how Zope can be used to build a simple Web application, and illustrated its power and flexibility as a rapid development tool for the Web medium. See ya! Note: All examples in this article have been tested on Linux/i586 with Zope 2.5. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |