服務熱線
153 8323 9821
對象:
request對象:獲取客戶端信息
request.form方法:獲取客戶端表單信息
form格式:
<form name="xxx" method=post action="xxx" '當action值為空時,表示把表單數據傳給自己
...
<input type="text" name="x"> '表示文本框
...
<input type="submit" value="提交"> '表示提交按鈕
...
<input type="cancel" value="重置"> '表示重置(或取消)按鈕
...
<input type="checkbox" name="zzz" value="讀書"> '表示復選框
<input type="checkbox" name="zzz" value="寫字"> '常多個寫在一起,表示復選
...
<testarea name="xxx" rows="3" cols="40"></testarea> '表示文本域
...
</form>
格式:變量=request.form(參數)
數據獲取方式一般為:post(method=post)
例1:獲取表單數據
test_1.asp
<form name="test" method="post" action="test4_2.asp"> <!-- form必備3個屬性:name、mathod="post"、action -->
請輸入您的姓名:
<input type="text" name="user_name"> <!-- 元素一:文本框,名稱:user_name -->
<input type="submit" value="提交"> <!-- 元素二:提交按鈕,值:提交 -->
</form>
test_2.asp
<%
Dim uname '定義變量
uname=request.Form ("user_name") 'request.form格式:request.form(參數);request.form接收的參數必須與form的元素一名稱一致,否責就無法接收到數據!
response.write uname & "!您好,歡迎您!"
改寫例1:
把test4_1.asp和test4_2.asp合并成一個asp文件test4_5.asp
<form name="test" method="post" action=""> <!-- 如果是自己提交給自己的話action的值為空! -->
請輸入您的姓名:
<input type="text" name="user_name"> <!-- 元素一:文本框,名稱:user_name -->
<input type="submit" value="提交"> <!-- 元素二:提交按鈕,值:提交 -->
</form>
<%
if request.form("user_name")<>"" then
Dim uname '定義變量
uname=request.Form ("user_name") 'request.form格式:request.form(參數);request.form接收的參數必須與form的元素名稱一致,否責就無法接收到數據!
response.write uname & "!您好,歡迎您!"
end if
%>
%>
例2:計算a+b=?
test4_3.asp
<form name=test method="post" action="test4_4.asp">
a<input type="text" name="a"> <!-- 元素一:文本框,名稱:a -->
+
b<input type="text" name="b"> <!-- 元素二:文本框,名稱:b -->
<p>
<input type="submit" value="提交"> <!-- 元素三:提交按鈕,值:提交 -->
</form>
test4_4.asp
<%
Dim a,b,c
a=request.Form("a") '接收文本框a中的數據
b=request.Form("b") '接收文本框b中的數據
c=CInt(a)+CInt(b) '用form方法接收的文本框中的數據為字符型!所以要把字符型轉化為數值型進行計算!
response.write "a+b的和=" & CStr(c) '輸出時要把數值型轉化為字符型!
%>
改寫例2:
把test4_3.asp和test_4.asp合并成一個asp文件test4_6.asp
<form name=test method="post" action=""> <!-- action的值為空! -->
a<input type="text" name="a"> <!-- 元素一:文本框,名稱:a -->
+
b<input type="text" name="b"> <!-- 元素二:文本框,名稱:b -->
<p>
<input type="submit" value="提交"> <!-- 元素三:提交按鈕,值:提交 -->
</form>
<%
If request.Form("a")<>"" And request.Form("b")<>"" then
Dim a,b,c
a=request.Form("a") '接收文本框a中的數據
b=request.Form("b") '接收文本框b中的數據
c=CInt(a)+CInt(b) '用form方法接收的文本框中的數據為字符型!所以要把字符型轉化為數值型進行計算!
response.write "a+b的和=" & CStr(c) '輸出時要把數值型轉化為字符型!
End if
%>
例3:根據用戶選擇分別重定向到老師和學生界面(request.form和request.redirect小綜合)
test4_10.asp
<form name="user" method="post" action="test4_14.asp">
請選擇你的類型:
<Select name="usertype">
<option value="學生">學生</option>
<option value="老師">老師</option>
</Select>
<p>
<input type="submit" value="提交">
</form>
test4_14.asp
<%
dim user_type
user_type=request.Form("usertype")
if user_type="學生" Then
response.redirect "student.asp"
Else
response.redirect "teacher.asp"
End If
%>
teacher.asp
<%="這是教師界面"%>
student.asp
<%=這是學生界面%>