萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP QueryString 取得 GET 表單資料教學

前言

當 HTML 表單使用 method="GET" 提交時,資料會以查詢字串形式附加在 URL 後方(如 tizagGet.asp?Name=John&Age=25)。ASP 透過 Request.QueryString 集合即可取得這些值。

HTML 表單範例

<form method="GET" action="tizagGet.asp">
  Name <input type="text" name="Name" />
  Age <input type="text" name="Age" />
  <input type="submit" />
</form>

ASP 處理頁面 (tizagGet.asp)

<%
Dim name, age
name = Request.QueryString("Name")
age  = Request.QueryString("Age")
Response.Write("Name: " & name & "
") Response.Write("Age: " & age & "
") %>

關鍵點

  • Request.QueryString("欄位名稱") 取得字串值,若欄位不存在回傳空字串。
  • 查詢字串會出現在 URL、瀏覽器歷史、伺服器記錄中,不適合傳遞敏感資料。
  • URL 長度限制(約 2,000 字元),大量資料請改用 POST。
  • 中文參數需注意編碼:瀏覽器送出時通常為 UTF-8,ASP 預設可能為系統代碼頁(如 Big5/GB2312),建議統一使用 UTF-8 儲存頁面並設定 Response.CodePage = 65001。

進階用法

<%
' 取得所有查詢字串鍵值
For Each key In Request.QueryString
    Response.Write(key & " = " & Request.QueryString(key) & "
") Next ' 同名多值(如 checkbox 群組)取得陣列 Dim vals : vals = Request.QueryString("colors") ' 回傳 "red,blue,green" Dim arr : arr = Split(vals, ",") %>
copyright © 萬盛學電腦網 all rights reserved