在 href onclick 中调用 VBScript Sub(经典 ASP)

发布于 2025-01-02 06:03:07 字数 1213 浏览 1 评论 0原文

我正在尝试用经典的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2025-01-09 06:03:07

因为它是 ASP,所以您必须将要在服务器上运行的内容与要在客户端上运行的内容分开。这在你的例子中并不清楚。假设,我假设您的数据库位于防火墙后面并且只能由服务器访问。

  • 任何需要在服务器上运行的内容都应该在 <% ... %> 中。块
  • ,即您的 insertdata() 例程可能属于服务器(当前它是从客户端运行)
  • 对服务器和会话的引用应该在服务器上完成,即在 <% ... %> 中块

其他一些注意事项:

  • 由于 insertdata() 不返回值,因此使用 Sub 而不是 Function ,
  • 主体和表单标记是错误的方式,
  • 事实上您不需要表单标记,

我会猜测提供的asp 脚本是服务器对您尚未向我们展示的表单的响应?不是很清楚,但也许您应该阅读 w3Schools 的 ASP 表单集合

您的 Function insertdata() 不正确。您正在混合客户端和服务器概念。例如,Session.Contents 必须在服务器端进行评估,并且必须位于 <% ... %> 内。块,而 insertdata() 函数本身属于客户端,因为正如您所指出的,您需要它对用户事件做出反应。我修改了你的 insertdata 的一行来说明我的观点:

Sub insertdata
    ...
    strSQL = "INSERT INTO Authen_User VALUES ("
    strSQL = strSQL & "'<%=Session.Contents("userKey")%>'"
    strSQL = strSQL & ",'<%=Session.Contents("name")%>'"
    strSQL = strSQL & ",'<%=Session.Contents("Type")%>')"
    ...
End Sub

当 ASP 看到你的 <% ... %>它将在服务器上用正确的值替换它们,以便当客户端看到它时,当网页到达客户端时,替换值将出现:

Sub insertdata
    ...
    strSQL = "INSERT INTO Authen_User VALUES ("
    strSQL = strSQL & "'userKey12345678'"
    strSQL = strSQL & ",'John Smith'"
    strSQL = strSQL & ",'Mr')"
    ...
End Sub

即,如果用户执行查看页面源在他的浏览器中,他永远不会看到 <%...%>在源中但被替换的值。因此,当他点击链接时,它会做正确的事情。

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.

  • Anything that needs to be run on the server should be in <% ... %> blocks
  • i.e. your insertdata() routine probably belongs on the server (currently it's run from the client)
  • Reference to Server and Session should be done on the server i.e. in the <% ... %> blocks

Some other notes:

  • Since insertdata() doesn't return a value, use Sub instead of Function
  • the body and form tags are the wrong way round
  • in fact you don't need a form tag

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 example Session.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:

Sub insertdata
    ...
    strSQL = "INSERT INTO Authen_User VALUES ("
    strSQL = strSQL & "'<%=Session.Contents("userKey")%>'"
    strSQL = strSQL & ",'<%=Session.Contents("name")%>'"
    strSQL = strSQL & ",'<%=Session.Contents("Type")%>')"
    ...
End Sub

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:

Sub insertdata
    ...
    strSQL = "INSERT INTO Authen_User VALUES ("
    strSQL = strSQL & "'userKey12345678'"
    strSQL = strSQL & ",'John Smith'"
    strSQL = strSQL & ",'Mr')"
    ...
End Sub

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文