ASP初級教程之ASP對錶單和使用者輸入的處理

來源:互聯網
上載者:User
教程

在ASP中,你可通過VBScript和其他方式調用自程式。

執行個體:

使用method="get"的表單
如何使用Request.QueryString命令與使用者進行互動。
<html><body><form action="/example/aspe/demo_aspe_reqquery.asp" method="get">Your name: <input type="text" name="fname" size="20" /><input type="submit" value="Submit" /></form><%dim fnamefname=Request.QueryString("fname")If fname<>"" Then      Response.Write("Hello " & fname & "!<br />")      Response.Write("How are you today?")End If%></body></html>
使用method="post"的表單
如何使用Request.Form命令與使用者進行互動。
<html><body><form action="/example/aspe/demo_aspe_simpleform.asp" method="post">Your name: <input type="text" name="fname" size="20" /><input type="submit" value="Submit" /></form><%dim fnamefname=Request.Form("fname")If fname<>"" Then      Response.Write("Hello " & fname & "!<br />")      Response.Write("How are you today?")End If%></body></html>
使用選項按鈕的表單
如何使用Request.Form通過選項按鈕與使用者進行互動。
<html><%dim carscars=Request.Form("cars")%><body><form action="/example/aspe/demo_aspe_radiob.asp" method="post"><p>Please select your favorite car:</p><input type="radio" name="cars"<%if cars="Volvo" then Response.Write("checked")%>value="Volvo">Volvo</input><br /><input type="radio" name="cars"<%if cars="Saab" then Response.Write("checked")%>value="Saab">Saab</input><br /><input type="radio" name="cars"<%if cars="BMW" then Response.Write("checked")%>value="BMW">BMW</input><br /><br /><input type="submit" value="Submit" /></form><%if cars<>"" then   Response.Write("<p>Your favorite car is: " & cars & "</p>")end if%></body></html>

使用者輸入

Request對象可用於從表單取回使用者資訊。

表單一實例:

<form method="get" action="simpleform.asp">First Name: <input type="text" name="fname" /><br />Last Name: <input type="text" name="lname" /><br /><br /><input type="submit" value="Submit" /></form>

使用者輸入的資訊可通過兩種方式取回:Request.QueryString 或 Request.Form。

Request.QueryString

Request.QueryString命令用於通過method="get"來搜集表單中的值。使用GET方法從表單傳送的資訊對所有的使用者都是可見的(出現在瀏覽器的地址欄),並且對所發送資訊的量也有限制。

如果某使用者在上面的表單一實例中輸入"Bill"和"Gates",發送至伺服器的URL。

假設ASP檔案"simpleform.asp"包含下面的代碼:

<body>Welcome<%response.write(request.querystring("fname"))response.write(" " & request.querystring("lname"))%></body>

瀏覽器將顯示如下:

Welcome Bill Gates

Request.Form

Request.Form命令用於使用"post"方法搜集表單中的值。使用POST方法從表單傳送的資訊對使用者是不可見的,並且對所發送資訊的量也沒有限制。

如果某使用者在上面的表單一實例中輸入"Bill"和"Gates",發送至伺服器的URL。

假設ASP檔案"simpleform.asp"包含下面的代碼:

<body>Welcome<%response.write(request.form("fname"))response.write(" " & request.form("lname"))%></body>

瀏覽器將顯示如下:

Welcome Bill Gates

表單驗證

只要有可能,就應該對使用者輸入的資料進行驗證(通過用戶端的指令碼)。瀏覽器端的驗證速度更快,並可以減少伺服器的負載。

如果使用者資料會輸入到資料庫中,那麼你應該考慮使用伺服器端的驗證。有一種在伺服器端驗證表單的好的方式,就是將(驗證過的)表單傳回(post)表單頁面,而不是轉至不同的頁面。使用者隨後就可以在同一個頁面中得到錯誤資訊了。這樣做的話,使用者就更容易發現錯誤了。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.