|
|
Building a Basic Web Form A common task you will might want to execute is to have someone fill out a form on a web page and when they click the submit button the data on the form gets parsed and emailed to someone else with a confirmation screen being displayed to the person who typed in the data that they form submission was successful. While this may sound complex, in reality the programming behind the email form is rather simple, because of the powerful WebBatch functions which handle. Let's start by looking at a simple form that can be created with simple HTML : <html> <head> <title>SimpleForm</title> </head> <body> <form action="" method="POST"> Name: <br> <input type="text" name="yourname"><br> <br> Email: <br> <input type="text" name="emailaddress"><br> <br> Comments: <br> <textarea name="comments" rows="20" cols="60"></textarea><br><br> <input type="submit" value="Submit"> </form> </body> </html> if you save this code as an HTML document and serve with with your HTTP server, you should see a screen on your browser, very similar to this : |
Copyright by darkchip.com |
At first glance, this form seems rather complete. The form has textareas, labeled fields and even a submit button. However I suspect that very soon you will realize that what you see is mostly an illusion, caused by only using HTML to serve this form. Pressing the Submit button does nothing and the form merely sits there. The reason why is that there is not any associated web script that gets executed when someone presses the Submit button. It should be no surprise that we will be using WebBatch to create this web script. Let's first off start by converting the simple HTML document to Webbatch : webout('Content-type: text/html',2) webout('<html>',1) webout('<head>',1) webout('<title>SimpleForm</title>',1) webout('</head>',1) webout('<body>',1) Webout('<form action="" method="POST">',1) Webout('Name: <br>',1) Webout('<input type="text" name="yourname"><br>',1) Webout('<br>',1) Webout('Email: <br>',1) Webout('<input type="text" name="emailaddress"><br>',1) Webout('<br>',1) Webout('Comments: <br>',1) Webout('<textarea name="comments" rows="20" cols="60"></textarea><br><br>',1) Webout('<input type="submit" value="Submit">',1) Webout('</form>',1) webout('</body>',1) webout('</html>',1) You will notice right away, that all we really did was place a bunch of webout commands that simply pass the contents in between quotes to the HTTP server to be parsed. If you save this code as a .web file (ie. SimpleForm.web) under the Webbatch directory you will then be able to called it from a browser and have Webbatch execute the script : |
In addition to simply adding webout commands in front of the raw HTML, you might have also noticed the addition of a single line at the beginning : webout('Content-type: text/html',2). The line Content-type: text/html is special piece of text that must be the first thing sent to the browser by any CGI script (web script) . As long as you remember to do that, everything will be fine. If you forget, the browser will reject the output of the script. Without going into too much detail that you really do not need to worry about, the Content-type: text/html statement in a web script is required when you are using any language to develop web scripts (perl, jsp,webbatch). Simply ensure that you have this being outputed at the start of the your web script. |
In the next section we will add all the web script logic to be able to do basic data validation and send the form data to an email address |