简体   繁体   English

调用vbscript函数以从在ASP经典页面上生成的HTML表单更新SQL表

[英]Calling a vbscript funciton to update a SQL table from an HTML form generated on an asp-classic page

Having a hard time with updating a SQL table from an HTML form generated on an asp-classic page. 很难从asp-classic页面上生成的HTML表单更新SQL表。 The page dynamically creates a table based on a SQL query; 该页面基于SQL查询动态创建一个表。 I've added a form for the purpose of editing certain fields displayed in the table but have been unable to trigger the function (ie onclick) that the form button calls using the code below. 我已经添加了一个表单,用于编辑表中显示的某些字段,但是无法使用下面的代码触发表单按钮调用的功能(即onclick)。 I've tried multiple variations of the syntax used for calling the function via VBscript and am missing something entirely. 我尝试了用于通过VBscript调用函数的语法的多种变体,并且完全遗漏了一些东西。 I'm wondering if I'm trying something that can't be done in the asp-classic environment due to the server side code being used and the limitations that go with that (as well as my trying to use the same page to generate the results). 我想知道我是否正在尝试由于使用服务器端代码及其附带的限制(以及我尝试使用同一页面来生成)而在asp-classic环境中无法完成的操作结果)。 I know the default behavior for the form is to refresh the page if I don't specify a different page. 我知道该表单的默认行为是,如果我未指定其他页面,则刷新页面。 In the code below, I'm just trying to confirm the behavior before I add the SQL update statement to the function. 在下面的代码中,我只是想确认行为,然后再将SQL更新语句添加到函数中。 (I'm typically only retrieving or inserting data for a web site - very little experience updating the data between a single page like this.) (我通常只检索或插入网站数据-很少有像这样在单个页面之间更新数据的经验。)

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

You're trying to mix server-side code with client-side code, and It Don't Work Like That TM . 您正在尝试将服务器端代码与客户端代码混合使用,并且它不会像TM那样工作。

Here's a very rough outline of how you could get information from the user, process it, and display it, all on the same asp page. 这是一个非常粗略的概述,说明如何从用户那里获取信息,对其进行处理以及将其显示在同一asp页面上。 Note the utter lack of JavaScript. 注意完全缺乏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
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM