我试图在我的网页上完成某些事情时生成一些验证警告框,并且出于某种原因,我将所有数据库调用放入一个单独的包含文件后我的Javascript停止工作(javascript在我执行此操作之前正在工作)香港专业教育学院尝试了我能想到的一切,到目前为止没有任何工作,所以我来到最后的边界寻求帮助,这是我的html表单启动数据库调用:
<form name="requestHol" action="newbooking.asp" method="post">
<div id="datepicker">
<label for="from">From </label>
<input type="text" id="from" name="from" readonly="readonly" >
<label for="to">to </label>
<input type="text" id="to" name="to" readonly="readonly" >
</div>
<div id="reason">
<textarea id="reasonInput" name="RequestComments" placeholder="Please enter the reason for your absence." maxlength="45" ></textarea>
</div>
<div id="reqbutton">
<button name="submitHolBtn" value="requestButton" type="submit">Submit Request</button>
</div>
</form>
这是我的ASP数据库调用:
If(Request.Form("submitHolBtn"))<>""Then
If(Request.Form("from"))="" or (Request.Form("to"))="" or (Request.Form("RequestComments"))="" Then
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Please make sure you fill in ALL inputs when booking a holiday.""); location.href = 'newbooking.asp'</SCRIPT>")
else
Set objDBConn = Server.CreateObject("ADODB.Connection")
objDBConn.Open Application("ConnString")
Set objDBCommand = Server.CreateObject("ADODB.Command")
objDBCommand.ActiveConnection = objDBConn
objDBCommand.CommandText = "spNewHoliday"
objDBCommand.CommandType = adCmdStoredProc
objDBCommand.Parameters.Append objDBCommand.CreateParameter("@StartDate", adDate, adParamInput,200)
objDBCommand.Parameters.Append objDBCommand.CreateParameter("@EndDate", adDate, adParamInput,200)
objDBCommand.Parameters.Append objDBCommand.CreateParameter("@EmployeeID", adVarChar, adParamInput,200)
objDBCommand.Parameters.Append objDBCommand.CreateParameter("@Reason", adVarChar, adParamInput,200)
objDBCommand.Parameters.Append objDBCommand.CreateParameter("@JobRoleID", adVarChar, adParamInput,200)
objDBCommand("@StartDate") = Request.Form("from")
objDBCommand("@EndDate") = Request.Form("to")
objDBCommand("@EmployeeID") = Session("UserID")
objDBCommand("@Reason") = Request.Form("RequestComments")
objDBCommand("@JobRoleID") = Session("JobRoleID")
Set objDBRS = Server.CreateObject("ADODB.RecordSet")
objDBRS.open objDBCommand,,adOpenForwardOnly
Session("IsBookingValid") = objDBRS(0)
SELECT CASE Session("IsBookingValid")
Case "HolidayBooked"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Holiday has been requested.""); location.href = 'newbooking.asp'</SCRIPT>")
Case "TooManyPeopleOfFSameJob"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Holiday CANNOT be booked, Max amount of employees with the same job role are off.""); location.href = 'newbooking.asp'</SCRIPT>")
Case "TooManyPeopleOff"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Holiday CANNOT be booked, Max amount of employees are already off.""); location.href = 'newbooking.asp'</SCRIPT>")
Case "YouHaveAlreadyBookedThisDateOff"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""You have already booked these dates off.""); location.href = 'newbooking.asp'</SCRIPT>")
Case "YouCanOnlyBookAMaxOf10Days"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""You can only book a max of 10 days off at a time.""); location.href = 'newbooking.asp'</SCRIPT>")
Case "YouDontHaveEnoughHolidaysLeft"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""You do not have enough days left to book that holiday off.""); location.href = 'newbooking.asp'</SCRIPT>")
Case "YouHaveAlreadyBookedAHolidayInThatPeriod"
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""You already have a holiday booked off during that time period.""); location.href = 'newbooking.asp'</SCRIPT>")
END SELECT
Set objDBCommand=nothing
objDBConn.Close
Set objDBConn=nothing
end if
end if
任何和所有帮助表示赞赏:)
答案 0 :(得分:1)
您想要使用Javascript验证表单客户端,还是使用经典asp验证服务器端。如果您想在客户端执行此操作,那么您的asp代码就无关紧要了,您需要在表单中添加一些javascript。
如果您想验证服务器端,那么您最好的选择是将您的插入代码和表单放在同一页面上,并将asp放在表单之前 - 例如
<%
If(Request.Form("submitHolBtn"))<>""Then
If(Request.Form("from"))="" or (Request.Form("to"))="" or (Request.Form("RequestComments"))="" Then
response.Write "<p class=""alerttext"">Please make sure you fill in ALL inputs when booking a holiday.</p>"
else
'your db code and cases
end if
end if
%>
<form name="requestHol" method = "post">
<div id="datepicker">
<label for="from">From </label>
<input type="text" id="from" name="from" value="<%=Request.Form("from")%>" readonly="readonly" >
<label for="to">to </label>
<input type="text" id="to" name="to" value="<%=Request.Form("to")%>" readonly="readonly" >
</div>
<div id="reason">
<textarea id="reasonInput" name="RequestComments" placeholder="Please enter the reason for your absence." maxlength="45" ><%=Request.Form("RequestComments")%>"</textarea>
</div>
<div id="reqbutton">
<button name="submitHolBtn" value="requestButton" type="submit">Submit Request</button>
</div>
<form>
我根本不会使用js作为警报,只是文字。由于代码是在服务器端处理的,它只会在相关的页面上显示,并且您可以一次显示多条错误消息而不是一串js警报
答案 1 :(得分:0)
你的问题在这里
If(Request.Form("from"))="" or (Request.Form("to"))="" or (Request.Form("RequestComments"))="" Then
response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Please make sure you fill in ALL inputs when booking a holiday.""); location.href = 'newbooking.asp'</SCRIPT>")
else
如果你的任何request.form空页面将被重新加载得太快以至于你甚至都看不到任何东西 - 它又是旧页面。使用response.end为每个Request.Form执行response.write,以查看您向页面提交的内容。 另外看看Session(&#34; IsBookingValid&#34;)有什么价值,如果有的话...... 在你的地方,我会在案件结束之前提出:
case else
response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('You have nothing for ASP to work with.');</SCRIPT>")
END SELECT
location.href =&#39; newbooking.asp&#39;当你重定向到其他地方时,这是一个很好的事情,但是因为你发布到同一个页面你不需要它,更多的它会让你感到困惑。只需确保你的response.write在正文中编写javascript而不是在标题中。
当您将asp代码移动到包含文件时,您是否偶然删除了日历的JS文件?如果是这样,那么你的所有日期选择器字段TO和FROM都将为空......