坚持使用经典的ASP表单提交

时间:2015-07-08 11:22:02

标签: asp-classic

我是经典asp的新手。我已经开发了一个简单的asp表单,我正在进行表单提交以访问数据库。我得到了一个代码来对字段进行表单验证,但代码只有在我使用相同的asp代码时才有效。当我将表单方法更改为不同的asp文件时,验证不起作用。

Form.asp

....code
<%
    Dim send, txtengineer, txtcaseid, txtassetno, txtusername, txtgroup, txtLOB, txtmodel, txtsim, countError
    send = Request.Form("submit")
    txtengineer = Request.Form("engineer")
    txtcaseid = Request.Form("caseid")
    txtasset = Request.Form("asset")
    txtusername = Request.Form("username")
    txtgroup = Request.Form("group")
    txtLOB = Request.Form("lob")
    txtmodel = Request.Form("model")
    txtsim = Request.Form("sim")
    countError = 0
%>
<form method="post" action="form.asp">
....
<%
            if send <> "" AND txtcaseid = "" then
                Response.Write "<span class=""message""> << Enter Case no.</span>"
                countError = countError + 1
            end If
%>

DB.asp

这是我想提交收集的字段值的代码。哪个工作正常。我不得不留下我想做的事情

我想先验证然后再提交表单。我知道我做的事情很愚蠢。

需要一些指导来修复代码块。 感谢。

1 个答案:

答案 0 :(得分:1)

将表单提交到其他页面的问题是,如果有错误,您会怎么做?告诉用户点击&#34;返回&#34;按钮?仅仅因为这个原因,我建议将DB代码放在与表单相同的页面中。如果您不能这样做,则需要将服务器端验证移至数据库页面。客户端验证仍将在表单页面上。 (请注意,如果您只进行一种类型的验证,则必须是服务器端。)

如果您在一个页面中执行所有操作,基本结构将如下所示:

<html><head>[etc.]
<%
Const errstr = "<span class='err'>*</span>"
Dim txtengineer, txtcaseid '[etc.]
Dim counterror(8) '- 8 = number of fields
counterror(0) = 0
If Request.Form <> "" Then
    txtengineer = Request.Form("engineer")
    'etc. with other fields
    Validate
    If counterror(0) = 0 Then
        SavetoDB
        Response.Redirect "Success.asp"
    End If
End If
Sub Validate()
    If txtengineer = "" Then
      counterror(1) = 1
      counterror(0) = counterror(0) + 1
    Else
      'do whatever other cleanup/validation you need
    End If
    'etc. for other fields
End Sub
Sub SavetoDB()
 'code to write stuff to database goes here
End Sub
%>
<script>
function validate(){
//client-side validation goes here
}
</script>
</head>
<body>
<%
If counterror(0) > 0 Then
    Response.Write "<p>Please fill out the required fields (*) below.</p>"
End If
%>
<form method="post" action="form.asp">
<p>Engineer: <input type="text" name="engineer" value="<%=txtengineer%>" size="30" maxlength="50">
   <%If counterror(1) = 1 Then Response.Write errstr%>
</p>
[etc. with other fields]
<p><input type="submit" name="Btn" value="Submit" onclick="validate();"></p>
</form>
</body>
</html>