加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

ASP初级教程之ASP对表单和用户输入的处置

发布时间:2023-09-07 12:52:44 所属栏目:Asp教程 来源:
导读:在ASP中,你可通过VBScript和其他方式调用自程序。实例:使用method="get"的表单如何使用Request.QueryString命令与用户进行交互。以下为引用的内容:<html><body><form action="/example/aspe/demo_aspe_reqquery.a

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

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

fname=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 cars

cars=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)表单页面,而不是转至不同的页面。用户随后就可以在同一个页面中得到错误信息了。这样做的话,用户就更容易发现错误了。

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章