10.6 Posted Data and fdat


10.6 Posted Data and %fdat

Posted data ”that is, data posted to the server by the client browser (this data was obtained using the param() function in the CGI and mod_perl chapters) ”is accessed by Embperl programs through the variable %fdat . Each form widget name is a key in %fdat , and the widget value is the value of that key.

Here's a simple example. First, there's a form to collect some data ( /var/www/html/embperl/fdat1.html ):

 <html>  <head>  <title>Form to Demonstrate %fdat</title>  </head>  <body bgcolor="#ffffff">  <h1>A Form to Demonstrate %fdat</h1>  <form action="/embperl/fdat2.html" method="post">  Name:  <input type="text" name="name"> <br>  Age:   <input type="text" name="age"> <br>  Phone: <input type="text" name="phone"> <br>  <input type="submit">  </form>  </body>  </html> 

There is nothing new about this; it is simply a form with three text input fields. No Embperl here. Still, Apache processes it with HTML::Embperl because it is under the /embperl subdirectory of the /var/www/html/ directory.

This is a consequence of the earlier decision to process all .html files under /embperl with HTML::Embperl, at a slight reduction in processor efficiency (but one hopes, an increase in programmer efficiency). This might be a bad choice if you're writing a server for HugeOnlineRetailer.com , but in that case, you'd probably know to make a different choice.

In this form, the action is /embperl/fdat2.html , an Embperl HTML file. Even though data is posted, we don't need CGI, as we did in Chapter 7. Embperl allows us to accomplish the same thing within an HTML page.

This is the page that processes the posted data ( /var/www/html/embperl/fdat2.html ):

 [-     # grab the posted data from %fdat      $name  = $fdat{name}   ;      $age   = $fdat{age}    ;      $phone = $fdat{phone}  ;  -]  <html>  <head>  <title>Processing %fdat</title>  </head>  <body bgcolor="#ffffff">  <h1>Processing %fdat</h1>  [$ if ($name ne  and $age ne  and $phone ne)$]      [# all the fields were filled out! #]  Thank you for filling out the form! You entered:  <br>Name = <b>[+ $name +]</b>  <br>Age = <b>[+ $age +]</b>  <br>Phone = <b>[+ $phone +]</b>  [$ else $]      [# uhoh, not all the fields were filled out #]  Sorry, but you need to go back and fill out all the fields.  [$ endif $]  </body>  </html> 

At the beginning of this file, the script gets the data from %fdat . When the data is posted, the value of the widget name is stored into $fdat{name} , the value of the widget age is stored into $fdat{age} , and the value of the widget phone is stored into $fdat{phone} .

Then, in the body of the HTML file, the script uses a [$ if ... $] command to check that it received values for all three widgets. If so, the data is printed to the user . If not, the script tells the client that they need to go back and correctly fill out the form.

To try this program, check either of these URLs: http://localhost/embperl/fdat1.html or www.opensourcewebbook.com/embperl/fdat1.html. If you enter some information into this form and click the submit button, you should see a result that resembles Figure 10.5.

Figure 10.5. Using %fdat

graphics/10fig05.gif



Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net