classic asp verify the server result

2019-09-14 23:38发布

问题:

I am trying to check or unchecked the check-boxes depends upon the data results that comes from server. But I cannot use below code correctly where I am doing wrong?

<%
Dim AFTER_SAVE, IN_VIEW, Y
Dim SQL, Data
SQL = " SELECT code, name, value FROM mytable WHERE code = '" & User & "'" 
Data = Data(SQL)
%>  
    <%If IsArray(Data) Then%>
         <%If ((Data(1,0) = "AFTER_SAVE") AND (Data(2,0) = "Y")) Then %>      
                document.getElementById("chkSave").checked == true;                      
         <%End If%>
         <% If ((Data(1,0) = "IN_VIEW") AND (Data(2,0) = "Y")) Then %>       
                document.getElementById("chkVIEW").checked == true;  
         <%End If%>
  <%End If%>

回答1:

You're trying to combine server-side code with client-side code in a very strange way. Sometimes, it's necessary to do that (i.e. use server-side VBScript to write client-side Javascript), but if I'm understanding your intent correctly, it's not needed here.

Basically, if this is actually a classic ASP page, then somewhere on that page you're generating the checkboxes in question. So all you need to do is put your database call somewhere before that, and then when you generate the checkboxes, you can output a checked='checked', or not, depending.

Note that I have no clue what Data = Data(SQL) is supposed to mean. There's no way for it to be valid VBScript code - parentheses are for arrays, but a string is not a valid array index, and then to assign it to itself like that? Anyway, I'm ignoring that part.

<html>
<head>
<%
Dim AFTER_SAVE, IN_VIEW
Dim SQL, RS, Conn
Dim User
'...blah blah blah, give a value to User, set up your DB connection, etc. etc....

SQL = "SELECT code, name, [value] FROM mytable WHERE code = '" & User & "'"
'- ("value" is a reserved keyword in SQL, hence the brackets)
Set RS = Server.Createobject("ADODB.Recordset")
RS.Open SQL, Conn, 1, 2 '- this is rather handwavy and unlikely to actually
'- work as-is; use the options and connection methods that make sense for you
Do Until RS.EOF
    '- I have no idea how your data is set up; this may make no sense.
    '- The idea is, read the checkbox states from your database, and 
    '- stick them in variables for later reference.
    Select Case RS("name")
        Case "AFTER_SAVE" AFTER_SAVE = RS("value")
        Case "IN_VIEW"   IN_VIEW = RS("value")
    End Select
    RS.Movenext
Loop
RS.Close
Set RS = Nothing
%>
</head>
<body>
<form method="post" action="myformhandler.asp">
<!-- form fields and stuff -->
<input type="checkbox" name="chkSave" id="chkSave" <%
If AFTER_SAVE = "Y" Then Response.Write "checked='checked'"
%> value="Y"><label for="chkSave">After save</label>
<input type="checkbox" name="chkView" id="chkView" <%
If IN_VIEW = "Y" Then Response.Write "checked='checked'"
%> value="Y"><label for="chkView">In view</label>
<!-- more form stuff -->
</form>
</body>
</html>


标签: asp-classic