调用 vbscript 函数从 asp-经典页面上生成的 HTML 表单更新 SQL table
Calling a vbscript funciton to update a SQL table from an HTML form generated on an asp-classic page
很难从 asp 经典页面上生成的 HTML 表单更新 SQL table。该页面根据 SQL 查询动态创建 table;我添加了一个表单,用于编辑 table 中显示的某些字段,但无法使用下面的代码触发表单按钮调用的功能(即 onclick)。我尝试了多种用于通过 VBscript 调用函数的语法变体,但完全遗漏了一些东西。我想知道我是否正在尝试一些在 asp-classic 环境中无法完成的事情,因为使用了服务器端代码以及随之而来的限制(以及我尝试使用生成结果的同一页面)。我知道如果我没有指定不同的页面,表单的默认行为是刷新页面。在下面的代码中,我只是想在将 SQL 更新语句添加到函数之前确认行为。 (我通常只是为网站检索或插入数据 - 很少有像这样在单个页面之间更新数据的经验。)
<br>
<form method="POST" id="form" action="">
<table>
<tr>
<td>
<select name="state">
<%
'populate the dropdown selection for the state within the form
do until rs.eof
For each x in rs.fields
response.write "<option value=" & x.Value & ">" & x.Value & "</option>" & vbNewLine
next
rs.movenext
loop
%>
</select>
</td>
<td>
<select name="ps">
<option value="blank"></option>
<option value="prime">Primary</option>
<option value="secondary">Secondary</option>
</select>
</td>
<td>
<input name="emp_num" size="5" maxlength="5" rows="1" ></textarea>
</td>
<td>
<input type="submit" onclick="Go" value="Update record"/>
</td>
</tr>
</table>
</form>
<%
function Go() {
msgbox "test"
//additional SQL code would be included here once the function actually works
}
%>
<%
'close the DB connection
dbconn.close
%>
</body>
</html>
您正在尝试将 server-side 代码与 client-side 代码混合,但它不会像那样工作TM。
下面是关于如何从用户那里获取信息、处理信息并显示信息的粗略概述,所有这些都在同一个 asp 页面上进行。请注意完全没有 JavaScript。 :)
<html>
<head>
<title>My Page</title>
<link rel="stylesheet" type="text/css" href="css/stylestuff.css">
<%
Dim rs, sql, conn
Dim mylist, myval1, myval2, i
myval1 = Request.Form("val1")
myval2 = Request.Form("val2")
'- If the form hasn't been submitted yet, myval1&2 will be the special value Empty,
'- but you can treat them as a blank string ("") for all intents and purposes.
If Validate(myval1, myval2) = True and myval1 & myval2 <> "" Then
DoStuffWith myval1, myval2
End If
%>
</head>
<body>
<h1>My Page</h1>
<form method="post" action="">
<p>Select one: <select name="val1" size="1">
<%
ListStuff mylist
For i = 0 to UBound(mylist,1)
response.write "<option value='" & mylist(0,i) & "'"
If myval & "" = mylist(0,i) & "" Then response.write " selected"
response.write ">" & mylist(1,i) & "</option>"
Next
%>
</select></p>
<p>Write something here:
<input type="text" name="val2" value="<%=myval2%>" size="10"></p>
<p><input type="submit" name="btn" value="Submit"></p>
</form>
<%
If myval1 & myval2 <> "" Then
DisplayStuff myval1, myval2
End If
%>
</body>
</html>
<%
Sub ListStuff(L)
sql = "Select [...] From [...] Where [...]"
Set rs = Server.Createobject("ADODB.Recordset")
rs.Open sql, "connection string (or previously-opened connection object)", 1,2
If Not rs.EOF Then
L = rs.GetRows
Else
Redim L(1,0)
L(0,0) = 0 : L(1,0) = "missing!"
End If
rs.Close
Set rs = Nothing
End Sub
'---------------------------------
Function Validate(v1,v2)
dim f
f = True
If Not Isnumeric(v1) Then f = False
If Len(v2) > 10 Then f = False
Validate = f
End Function
'---------------------------------
Sub DoStuffWith(v1,v2)
'- table-creation (or whatever) code goes here
End Sub
'---------------------------------
Sub DisplayStuff(v1,v2)
'- display code goes here
End Sub
%>
很难从 asp 经典页面上生成的 HTML 表单更新 SQL table。该页面根据 SQL 查询动态创建 table;我添加了一个表单,用于编辑 table 中显示的某些字段,但无法使用下面的代码触发表单按钮调用的功能(即 onclick)。我尝试了多种用于通过 VBscript 调用函数的语法变体,但完全遗漏了一些东西。我想知道我是否正在尝试一些在 asp-classic 环境中无法完成的事情,因为使用了服务器端代码以及随之而来的限制(以及我尝试使用生成结果的同一页面)。我知道如果我没有指定不同的页面,表单的默认行为是刷新页面。在下面的代码中,我只是想在将 SQL 更新语句添加到函数之前确认行为。 (我通常只是为网站检索或插入数据 - 很少有像这样在单个页面之间更新数据的经验。)
<br>
<form method="POST" id="form" action="">
<table>
<tr>
<td>
<select name="state">
<%
'populate the dropdown selection for the state within the form
do until rs.eof
For each x in rs.fields
response.write "<option value=" & x.Value & ">" & x.Value & "</option>" & vbNewLine
next
rs.movenext
loop
%>
</select>
</td>
<td>
<select name="ps">
<option value="blank"></option>
<option value="prime">Primary</option>
<option value="secondary">Secondary</option>
</select>
</td>
<td>
<input name="emp_num" size="5" maxlength="5" rows="1" ></textarea>
</td>
<td>
<input type="submit" onclick="Go" value="Update record"/>
</td>
</tr>
</table>
</form>
<%
function Go() {
msgbox "test"
//additional SQL code would be included here once the function actually works
}
%>
<%
'close the DB connection
dbconn.close
%>
</body>
</html>
您正在尝试将 server-side 代码与 client-side 代码混合,但它不会像那样工作TM。
下面是关于如何从用户那里获取信息、处理信息并显示信息的粗略概述,所有这些都在同一个 asp 页面上进行。请注意完全没有 JavaScript。 :)
<html>
<head>
<title>My Page</title>
<link rel="stylesheet" type="text/css" href="css/stylestuff.css">
<%
Dim rs, sql, conn
Dim mylist, myval1, myval2, i
myval1 = Request.Form("val1")
myval2 = Request.Form("val2")
'- If the form hasn't been submitted yet, myval1&2 will be the special value Empty,
'- but you can treat them as a blank string ("") for all intents and purposes.
If Validate(myval1, myval2) = True and myval1 & myval2 <> "" Then
DoStuffWith myval1, myval2
End If
%>
</head>
<body>
<h1>My Page</h1>
<form method="post" action="">
<p>Select one: <select name="val1" size="1">
<%
ListStuff mylist
For i = 0 to UBound(mylist,1)
response.write "<option value='" & mylist(0,i) & "'"
If myval & "" = mylist(0,i) & "" Then response.write " selected"
response.write ">" & mylist(1,i) & "</option>"
Next
%>
</select></p>
<p>Write something here:
<input type="text" name="val2" value="<%=myval2%>" size="10"></p>
<p><input type="submit" name="btn" value="Submit"></p>
</form>
<%
If myval1 & myval2 <> "" Then
DisplayStuff myval1, myval2
End If
%>
</body>
</html>
<%
Sub ListStuff(L)
sql = "Select [...] From [...] Where [...]"
Set rs = Server.Createobject("ADODB.Recordset")
rs.Open sql, "connection string (or previously-opened connection object)", 1,2
If Not rs.EOF Then
L = rs.GetRows
Else
Redim L(1,0)
L(0,0) = 0 : L(1,0) = "missing!"
End If
rs.Close
Set rs = Nothing
End Sub
'---------------------------------
Function Validate(v1,v2)
dim f
f = True
If Not Isnumeric(v1) Then f = False
If Len(v2) > 10 Then f = False
Validate = f
End Function
'---------------------------------
Sub DoStuffWith(v1,v2)
'- table-creation (or whatever) code goes here
End Sub
'---------------------------------
Sub DisplayStuff(v1,v2)
'- display code goes here
End Sub
%>