Chapter 4. HTML From

การจัดการ HTML Form

             

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

 วิธีทำ

  1. เขียน File Form ชื่อ order.html ตามตัวอย่างข้างล่าง
  2. ให้ post ไปที่ order.asp
  3. เขียน ASP ชื่อ order.asp ตามตัวอย่างข้างล่าง
  4. ทดสอบโดยนำ order.html ไปไว้ที่ Subdirectory c:\inetpub\wwwroot
  5. นำ order.asp ไปไว้ที่ Subdirectory c:\inetpub\wwwroot
  6. ไปที่ Browser พิมพ์ http://localhost/order.html

ตัวอย่าง file html

order.html

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

<body>

<form method="POST" action="order.asp">

<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>

ตัวอย่าง file ASP

order.asp

<html>
<
head>
<
title>ตัวอย่าง order.asp</title>
<
/head>

<body>

คุณ         <%=request.form("name")%><br>

E-mail <%=request.form("email")%><br>

สินค้าที่คุณต้องการสั่ง<br>

<%=request.form("book1")%><br>

<%=request.form("book2")%><br>

 

</body>

</html>

ทดลอง Run
                                     cgi_main_asp.gif (14635 bytes)

ผลลัพธ์

             out_php.gif (4685 bytes)

คำอธิบาย

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

      ใน file order.asp ได้กำหนดค่า request.form(" ") ภายในเครื่องหมาย " "   เราจะใส่ตัวแปรเช่นเดียวกับ order.html

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


Copy right Passkorn Roungrong 2000