在 href onclick 中调用 VBScript Sub(经典 ASP)
我正在尝试用经典的 asp 编写,以便在用户单击链接时将数据添加到数据库中。但是,我在调用 vbscript 过程时遇到问题。我尝试与 <%Sub/End %>
一起使用,并在 onclick
中使用 - onclick="<%insertdata()%>"
并且它在页面加载时运行,但我只想在用户单击时运行它。所以,我像下面这样改变了它,但它不起作用!这是我的代码:
<html>
<head>
<script language="vbscript">
Function insertdata()
Dim con
Dim objRs
Dim strSQL
Set con = Server.CreateObject("ADODB.Connection")
con.Open "DSN=***;UID=***;PWD=***"
strSQL="INSERT INTO Authen_User VALUES ('"&Session.Contents("userKey")&"','"&Session.Contents("name")&"','"&Session.Contents("Type")&"')"
set objRS = con.execute(strSQL)
con.Close
set con=Nothing
End Function
</script>
<title></title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<form><body>
<table><tr><td>
<a href="defaultpage.aspx?key="&Server.URLEncode(Session.Contents("userKey"))" language="vbscript" onclick="insertdata()">Semetral Survey</a><br>
<a href="login_2.asp">Return to Login Page</a>
</td></tr></table>
</body></form>
</html>
I'm trying to write in classic asp to add data into database when the user click on the link. But, I have problem with calling the vbscript procedure. I tried to use with <%Sub/End %>
and in onclick
I used - onclick="<%insertdata()%>"
and it runs on page load but I only want to run this when the user clicks. So, I changed it like below but it doesn't work! Here is my code:
<html>
<head>
<script language="vbscript">
Function insertdata()
Dim con
Dim objRs
Dim strSQL
Set con = Server.CreateObject("ADODB.Connection")
con.Open "DSN=***;UID=***;PWD=***"
strSQL="INSERT INTO Authen_User VALUES ('"&Session.Contents("userKey")&"','"&Session.Contents("name")&"','"&Session.Contents("Type")&"')"
set objRS = con.execute(strSQL)
con.Close
set con=Nothing
End Function
</script>
<title></title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<form><body>
<table><tr><td>
<a href="defaultpage.aspx?key="&Server.URLEncode(Session.Contents("userKey"))" language="vbscript" onclick="insertdata()">Semetral Survey</a><br>
<a href="login_2.asp">Return to Login Page</a>
</td></tr></table>
</body></form>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为它是 ASP,所以您必须将要在服务器上运行的内容与要在客户端上运行的内容分开。这在你的例子中并不清楚。假设,我假设您的数据库位于防火墙后面并且只能由服务器访问。
其他一些注意事项:
我会猜测提供的asp 脚本是服务器对您尚未向我们展示的表单的响应?不是很清楚,但也许您应该阅读 w3Schools 的 ASP 表单集合。
您的
Function insertdata()
不正确。您正在混合客户端和服务器概念。例如,Session.Contents
必须在服务器端进行评估,并且必须位于 <% ... %> 内。块,而 insertdata() 函数本身属于客户端,因为正如您所指出的,您需要它对用户事件做出反应。我修改了你的 insertdata 的一行来说明我的观点:当 ASP 看到你的 <% ... %>它将在服务器上用正确的值替换它们,以便当客户端看到它时,当网页到达客户端时,替换值将出现:
即,如果用户执行查看页面源在他的浏览器中,他永远不会看到 <%...%>在源中但被替换的值。因此,当他点击链接时,它会做正确的事情。
Because it's ASP you have to separate what you want to run on the Server and what you want to run on the client. This isn't clear in your example. Hypothetically, I would assume your database to be behind a firewall and only accessible to the server.
Some other notes:
I'm going to guess that the supplied asp script is the server response to a form that you haven't showed us? It's not very clear, but perhaps you should have a read of ASP Form Collection from w3Schools.
Your
Function insertdata()
is incorrect. You're mixing client and server concepts. For exampleSession.Contents
must be evaluated on the Server side and must be inside <% ... %> block, whilst the insertdata() function itself belongs on the client side because, as you indicate you need it to react to a user event. I've reworked one line of your insertdata to illustrate the point I'm making:When ASP sees your <% ... %> it will replace them on the server with the proper values so that by the time the client sees it the substitute values will appear when the web page arrives on the client:
i.e. if the user does a View page source in his browser he will never see the <%...%> in the source but the replaced values. So, when he clicks on the link it will do the right thing.