简体   繁体   中英

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. The page dynamically creates a table based on a SQL query; 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. I've tried multiple variations of the syntax used for calling the function via VBscript and am missing something entirely. 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). 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. (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 .

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. Note the utter lack of 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
%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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