Chapter 4. HTML From

การจัดการ HTML Form

             

             จากบทที่ผ่านมาเราได้ทดสอบเขียน Perl กันแล้ว แต่เขียนในรูปของ ภาษา Perl อย่างเดียว ยังมิได้เขียนร่วมกับ HTML Form ซึ่งนับว่าเป็นหัวใจสำคัญของ CGI (Command Gate Way Interface)  ก่อนเริ่มเขียน ผมขอแนะนำให้คนที่ยังไม่เคยเขียนหรือไม่เข้าใจเกี่ยวกับคำสั่ง Form ของ html ต้องกลับไปทบทวนเขียนให้คล่องเสียก่อน เพราะถือว่า Form เป็นประตูด่านแรกที่จะนำคุณเข้าสู้ CGI (Command Gate Way Interface) และเชื่อมต่อ PHP เรามาเริ่มกันเลย

 วิธีทำ

  1. เขียน File Form ชื่อ order.html ตามตัวอย่างข้างล่าง
  2. ให้ post ไปที่ Subdirectory /cgi-bin/order.pl
  3. เขียน File Perl ชื่อ order.pl ตามตัวอย่างข้างล่าง
  4. ทดสอบโดยนำ order.html ไปไว้ที่ Subdirectory c:\httpd\htdocs ( ขอพูดเฉพาะ windows 95/98 เพราะ Unix พี่ๆ  Admin คงเข้าใจกันดี)
  5. นำ order.pl ไปไว้ที่ Subdirectory c:\httpd\cgi-bin\
  6. ไปที่ Browser พิมพ์ http://localhost/order.html

ตัวอย่าง file html

order.html

<html>
<
head><title>ThaiWBI ShoppingForm สั่งซื้อสินค้า</title>
<
/head>

<body>

<form method="POST" action="/cgi-bin/order.pl">

<center><h3>ThaiWBI Shopping</h3><br>
<h1>Form สั่งซื้อสิ้นค้า</h1></center>

Name <input type="text" name="name"><br>
Email<input type="text" name="email"><p>

ต้องการสั่งซื้อหนังสือ<br>

<input type="checkbox" name="book1" value="Database On Web">Database On Web<br>
<input type="checkbox" name="book2" value="CGI PHP">CGI PHP<p>

<input type="submit" value="Order Now"><input type="reset" value="Reset">

</form>

</body>
<
/html>

เราจะนำ Code ต่อไปนี้เป็นตัวรับค่าตัวแปรจาก file order.html

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {

($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
if ($allow_html != 1) {
$value =~ s/<([^>]|\n)*>//g;
}$FORM{$name} = $value;

}

ตัวอย่าง file perl

order.pl

#/usr/bin/perl

print "Content-type: text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {

($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
if ($allow_html != 1) {
$value =~ s/<([^>]|\n)*>//g;
}$FORM{$name} = $value;
}

print "คุณ $FORM{'name'} <br>\n ";
print "
Email $FORM{'email'} <br>\n";

print "สินค่าที่คุณต้องการสั่ง <br>\n";
print "
$FORM{'book1'}<br>\n";
print "
$FORM{'book2'}<br>\n";

 

ทดลอง Run
                                     cgi_main_perl.gif (19442 bytes)

ผลลัพธ์

             out_php.gif (4685 bytes)

คำอธิบาย

      ใน file order.html เราได้กำหนดตัวแปรไว้ ดังนี้
$name = ชื่อผู้สั่งสินค้า
$email = email ของผู้สั่งสินค้า
$book1= สินค้าหนังสือ Database On Web
$book2= สินค้าหนังสือ CGI PHP

      ใน file order.pl ได้กำหนดตัวแปรเช่นเดียวกับ order.html

      เมื่อ order.html ส่งตัวแปรทั้งหมดที่ถูกเลือกและถูกใส่ข้อมูลลงไป จากนั้น order.pl ก็จะรับค่าตัวแปร และแสดงผลออกทางจอภาพ


Copy right Passkorn Roungrong 2000