![]() |
| ||
| Perl/CGI (Reading Data) Part II Prelude In the last tutorial, we covered a basic skeleton of a small CGI in Perl. It didn't do much, other than check if the user tried to go directly to the CGI, or if they were submitting information to us through a page. This doesn't do us a whole lot of good, when it comes to retrieving actual information. So, Now What? The answer is simple. We'll Use Param. Param When a web page is submitted, it uses 1 of 2 methods. Either GET or POST. With the Param function of CGI.pm, it doesn't really matter which method is used to pass the information to be submitted, as long as something is being submitted. Now, We Can Read That Information...As Long As We Know The Name Of The Form Element We Want To Read! While I, very well may be telepathic, the programs I write are not. So, if we don't know the name of the form element, we can't retrieve it's value. Plain and simple. If, however, you built the HTML Page that is submitting it's information to our CGI, then you should know the name of the form element. Let's Pretend that we have a web page, that has 2 textbox's and a submit button. The name of the first textbox, is "fname" for first name, and the name of the second textbox is "lname" for last name. Joe Someguy, has entered his information into the page, and has pressed submit! All we have to do is modify our previous if statement a little. Let's do that: if (param()) {chomp is a pretty simple function that removes white-spaces (newline characters, and spaces, basically) from the end of a string. In our case, the string is param('fname'). If you remember, fname is the name of the first name textbox that we have on our web page. Param, when passed the name of the form element, returns it's value. So, Basically, the variable $fname, now contains whatever Joe typed in the "fname" named textbox in our HTML, and $lname contains whatever was typed in lname. To Illustrate this, let's modify our start_page sub, just a little bit. We are going to think the person by name: sub start_page If you look carefully at the HTML Title Tag, and the HTML H1 Tag, You'll notice that we stuck our variable in there! Now, the page after accepting the submission from the user, will thank them for the submission by name (or whatever they typed in the fname box of the HTML Page)!!! Reading Data Conclusion Even though this CGI doesn't do anything spectacular, we are now able to obtain the information that the user has entered into textbox's, and hold them into variables. The hardest part is over! We checked for user submission, and if they submitted information, we gathered it into variables for use in our program! We Can now do just about anything with this information. We can save it to a file, or send it to ourselves in an e-mail. There are endless possibilities that a creative mind can come up with... next, we'll go over saving the information to a file for use at a later time. Here is the CGI so far: #!/usr/bin/perl |
| All times are GMT -4. The time now is 4:45 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC