Issue 65 February 12 2009 | ||||||
Handling Web Forms with Rev Web forms are everywhere. People use them for guest books, comments boxes, event registration, and many other purposes. If you're going to accomplish anything serious on the web, you'll eventually want to incorporate a form into your site. Yet even if you know HTML, forms can be a bit confusing, because they interact with the server in a way that static HTML pages do not. A visitor types something into a field, chooses an item from a popup menu, and then clicks a button to send the information to the server. What happens then? Well, a functioning form always works in conjunction with a program, residing on the Web server, which receives the information and does something with it. The program can do any number of things, such as checking the input for valid content, adding the content to a database, sending an email, showing a confirmation page, or all of the above. Now, a lot of programs are out there already, based on Perl or PHP, which handle forms. But a lot of them are either too simple and don't do what you need, or too complex and would take hours to understand. Modifying them can be hit-or-miss if you don't know the language they're written in. In this article, I'm going to show you how to use Revolution to accept input from forms and enjoy the benefits of using a language you already know to write functional server software. Forms in HTML A comprehensive discussion of HTML forms syntax could take several pages, so I'm just going to start with the basics here. Fortunately, most web authoring software (like FrontPage, Dreamweaver, etc.) make the construction of a form easy as pie. You probably have one of these tools lying around, and if you don't there's a bunch of free ones on the web, so just use that to build your form. At some point, though, you'll need to understand how the HTML is set up, so it helps if you've used HTML before. I'll use a simple example to highlight the most relevant aspects of a web form. Here's a form you might encounter in a Web browser: And here is the HTML code that produces the form above: <form method="" action=""> <p>First Name: <input type="text" name="fname" size="20"> Last Name: <input type="text" name="lname" size="20"></p> <p>Continent: <select size="1" name="cont"> <option value="1">North America</option> <option value="2">South America</option> <option value="3">Europe</option> <option value="4">Africa</option> <option value="5">Asia</option> <option value="6">Australia</option> <option value="7">Antarctica</option> </select></p> <p>Gender: <input type="radio" value="m" name="gender" checked>Male <input type="radio" value="f" name="gender">Female</p> <p><input type="checkbox" name="agreed" value="YES"> I have read and agreed to the terms of service</p> <p><input type="submit" value="Submit" name="submit"> <input type="reset" value="Reset" name="reset"></p> </form> The form consists of two input fields, a drop-down Option menu, two radio buttons, a check box, and two buttons. Note the following characteristics:
The Method Because the GET method is a little bit more "visible" and straightforward to process, we'll work with this method first, even though the sample form as presented here more resembles a data entry situation than a search. The Action Attributes of Form Elements The elements of a form which are buttons and menus have a "value" attribute. This is what selecting that button will place into the field name you've specified. For example, if the user clicks the "I have read and agreed..." check box, then the field "agreed" will have the value "YES," otherwise it will be empty. The two radio buttons, you notice, have the same name, "gender." This not only tells the browser that these radio buttons are in the same group (and therefore mutually exclusive, checking one disables the other), but put the appropriate value "m" or "f" into the gender field. The attribute "checked" indicates the default, preselected button. The options for the "Continent" drop-down menu each have a value associated with them, too. This means that when a user chooses "Europe" from the menu, the number 3 will be sent to the server, not the text "Europe." The two text input fields we have in this sample form do not have a "value" attribute; that's because their value will be determined by what the user enters into those fields at submission time. Submitting the Form web address ? name 1 = value 1 & name2 = value 2 & name 3 = value3... The question mark indicates the beginning of the URL parameters, which are a series of name/value pairs separated by ampersands... one pair for each field in the form. The web browser automatically encodes the data to conform to proper URL standards (for example converting spaces to "+" signs), and then sends it along to the server program you specified in the action attribute of the form. For example, if a user submitted a form
The form tag would read <form method="GET" action="ez-reg.cgi"> And the resulting URL after the user submission would be http://www.ex.com/reg/ez-reg.cgi?fname=Al&lname=Smith&cont=3&gender=m&agreed=YES Perhaps you can already see why POST is preferred for uploading data. For one, the URL is sent in clear text and shows up in Web server logs, etc. Second, it's not difficult to wind up with extremely long URLS this way. Nevertheless, it's an obvious format that is easily handled in your Revolution script. The Revolution Server Script For the next step, you'll need to have Revolution set up on a web server. If anyone asks you, "how do you set up Rev on a web server?" you can say, "Oh just drop the standalone engine up there and set the permissions." This will make you sound like a hip marketing person. Or, you can refer them to one of the excellent newsletter articles on this topic by Jacqueline Landman-Gay, such as this one. Seriously though, if you're using a properly-configured, Revolution-friendly hosting provider like Dreamhost, it's just about that easy. Otherwise, delve into Jacquie's article and website for the nitty-gritty. Needless to say, installing the engine and the basics of uploading Revolution CGIs is beyond the scope of this article. However, once you have Revolution installed on your server and successfully tested a "Hello World" CGI script, you're ready to tackle something really useful and not too difficult, like a script that handles form input. We have four fundamental tasks to perform with our sever form-handler script:
In an ideal world, it will be more complicated. You could wish to validate the data and give users an opportunity to correct mistakes. You might want to post the validated data to a database. You'll definitely want to use the POST method. We'll cover these topics in subsequent articles; for now these four steps will be rewarding enough. Retrieving the Submitted Data $QUERY_STRING Remember how "GET" was designed for querying databases? Well, when your script starts up, all that stuff in the URL now lives in the "query string," ready for you to act upon it. Transforming the Query String put $QUERY_STRING into x Pretty nice, eh? Revolution already provides a function to descramble any transformations done to the text entry to make it URL-compatible. These lines transform x into two columns with the field names on the left and their values on the right.Now I know some people will immediately think of arrays and using the split command. Those fancy pants probably already know how to do this stuff.Sending an Email on mailMe theMsg So there you have it. Open a process to sendmail, feed it the from, to, subject, and message text, close the connection, and wait till the buffer is cleared. The magic format? plain-English labels for each header item, a space after the colon, and a blank line between the headers and message text. Done.Feedback to the User Content-Type: text/html Where XXXX is the actual number of bytes you're going to send to the Web browser. Therefore we have some "sub-tasks" to perform with respect to giving user feedback:
For the purposes of this example, I'm going to keep things REAL simple. All I want is a simple message, "Thanks for your response, {First Name}. Click here to continue." Where "here" is a hyperlink to the home page of the site.Now, I could get real fancy and put an HTML file in the same directory as the form script, then read that file in, massage it, and spit out the merged page with the header info. But instead, I'm going with the "brute force" method and baking the HTML right into my solution. put "<HTML><HEAD></HEAD><BODY>" after thePage What does the whole script look like? #!rev3 -ui That wasn't too hard, was it?
|
|