Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources] - نسخه متنی

James Lee, Brent Ware

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










10.6 Posted Data and %fdat


Posted datathat 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/fdat1l):


<html>
<head>
<title>Form to Demonstrate %fdat</title>
</head>
<body bgcolor="#ffffff">
<h1>A Form to Demonstrate %fdat</h1>
<form action="/embperl/fdat2l" 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 l 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/fdat2l, 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/fdat2l):


[-
# 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/fdat1l or www.opensourcewebbook.com/embperl/fdat1l. 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



/ 136