Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

Order in which ASP code is read

I have a function which validates 2 dates;  Beginning Date and Ending Date.  It also validates another textbox called Num_Due.

For the most part, the function works ok.  After clicking the submit button, the function is called.  Even if I don't add an onclick event to the submit button, the function is still called which I don't understand why.  That is my first question.

The second problem and I guess biggest confusion is that in my function, I had to add 2 instances of the check for the Beginning Date field.  If I didn't have a double if statment....one before the Ending Date if statements and a copy of the Beginning date if statements after the Ending Date if Statments, what would happen is if I had put in a valid date for Ending date but not Beginning date, the page would submit ok even though I had a null value for Beginning Date.  After I put another copy of the if statement for Beginning Date and put that copy after the Ending date check, it solved my problem.

But I shouldn't have to put 2 of the same exact statements for the Beginning Date check.  It must have something to do with the order and how many times the function is run when I click submit is why it was not doing another run through on checking the Beginning Date field after a put a valid Ending Date in......I hope I haven't lost you at this point but just look at the code.  You are probably wondering why I put 2 instances of the Beginning date if statement in my ValidData function.....I too know that this is bad code but If I don't, the above will happen.

Also, I noticed that before without the duplicate if statment for the Beginning date, if I were to have put a valid Beginning date and no Ending date, it validated and checked the fields correctly telling me that I was missing and Ending date.  It is just when I have the senario the other way around which didn't work.


If EmployeeID = 0 Then
   Response.Redirect("not_auth.html")
End If

'__________________________________________________
'If data was valid, write data and send emial
'__________________________________________________

If Request("submitted") = 1 Then
   If ValidDataCheck = 1 Then
      Dim VacID
      VacID = SaveReqData
      SendMgrEmail VacID
      Response.Redirect("new_vacreq_ack.asp?vacid=" & VacID)
   End If
End If



If Request("Full_Time") = "ON" Then ChkFullTime = "checked"
If Request("Part_Time") = "ON" Then ChkPartTime = "checked"



Dim HireDate1, Rs3

Set Rs3 = Cn.Execute("select FirstName,[DateHired] from tblemployee where employeeid=" & _
         EmployeeID)

'__________________________________________________________________________
'Get the employee Id of the person logged into this session.  Grab it from
'NT and then compare it against the exchange table to pull in the Id -DAVE
'__________________________________________________________________________


'_____________________________________________
'Error Checking
'_____________________________________________

Function ValidDataCheck

 Dim Invalid
 
 If not isnull(request("BegDate")) Then
     If isdate(request("BegDate")) Then
             Invalid = 0
     Else
           Invalid = 1
           ErrMsg2 = "<tr><td COLSPAN=2><FONT SIZE=2 FACE=ARIAL COLOR=RED>" &_
               "<B>The Beginning Date is missing or is not a valid date</B></FONT></td></tr>"
         End If
 End If

 If not isnull(request("EndingDate")) Then
     If isdate(request("EndingDate")) Then
             Invalid = 0
     Else
           Invalid = 1
           ErrMsg3 = "<tr><td COLSPAN=2><FONT SIZE=2 FACE=ARIAL COLOR=RED>" &_
               "<B>The Ending Date is missing or is not a valid date</B></FONT></td></tr>"
         End If
End If

If not isnull(request("BegDate")) Then
     If isdate(request("BegDate")) Then
             Invalid = 0
     Else
           Invalid = 1
           ErrMsg2 = "<tr><td COLSPAN=2><FONT SIZE=2 FACE=ARIAL COLOR=RED>" &_
               "<B>The Beginning Date is missing or is not a valid date</B></FONT></td></tr>"
         End If
 End If

If Request("Num_Due") = "" Then
      If not IsNumeric(Request("Num_Due")) Then
            Invalid = 1
            ErrMsg1 = "<tr><td COLSPAN=2><FONT SIZE=2 FACE=ARIAL COLOR=RED>" &_
            "<B>You did not specify a valid Number of Days Due or it is missing</B></tr></td>"
      Else
            Invalid = 0
      End If
End If

If Invalid = 1 Then
      ValidDataCheck = 0
   Else
      ValidDataCheck = 1
   End If

End Function

'_____________________________________________
'Function to Save fields to DB
'_____________________________________________

Function SaveReqData

   Dim Rs, SQL, MgrNTAccount, DeptID
   Dim FT, PT
   If Request("Full_Time") = "ON" Then
      FT = 1
   Else
      FT = 0
   End If

   If Request("Part_Time") = "ON" Then
      PT = 1
   Else
      PT = 0
   End If


   Set Rs = Cn.Execute("select * from qryGetMgrNTAccountByEmployeeID where employeeid=" & _
      EmployeeID)

   If Not Rs.EOF Then MgrNTAccount = Rs("ntaccount")

   Set Rs = Cn.Execute("select [dept#] from tblemployee where employeeid=" & _
      EmployeeID)

'_________________________
'Insert Values into Table
'_________________________

   If Not Rs.EOF Then DeptID = Rs(0)
   Dim HireDate, Rs2

   Set Rs2 = Cn.Execute("select [DateHired] from tblemployee where employeeid=" & _
         EmployeeID)
   If not Rs.EOF Then HireDate = Rs2("DateHired")

   SQL = "insert into VacationRequest (employeeid,deptid,CreateDate,NumDaysDue,DateHired,mgr_nt_account," &_
   "FullTime,PartTime,Comments, RequestStart, RequestEnd) values('" & _
      EmployeeID & "','" & DeptID & "','" & Date() & "','" & _
      Replace(Request("Num_Due"),"'","''") & "','" & _
      HireDate & "','" & _
      MgrNTAccount & "','" & FT & "','" & PT & "','" & _
      Replace(Request("Comments"),"'","''") & "','" & _
      Replace(Request("BegDate"),"'","''") & "','" & _
      Replace(Request("EndingDate"),"'","''") & "')"

   Cn.Execute SQL

   Set Rs = Cn.Execute("select max(vacid) from VacationRequest where employeeid=" & EmployeeID)
   If Not Rs.EOF Then SaveReqData = Rs(0)

End Function


%>

<!-- #INCLUDE FILE="sendmail.inc" -->

<html>

<HEAD>

</HEAD>

<form method="POST" action="new_vacation_entry.asp" name=form1>
  <input type=hidden name=submitted value=1>
<BODY>

  <TABLE CELLPADDING=4 border="1" width="800">

<!---------------- TITLE ------------------------>

    <TR>
      <TD align=center COLSPAN=2><b>
      <FONT FACE="Arial" SIZE=5><IMG SRC="../images/vacation.jpg"><BR>
      Vacation Request Form</font></b>
      <BR><BR>
      </TD>
      <TD VALIGN=TOP ROWSPAN=9>
      &nbsp;&nbsp
      </td>
       <TD VALIGN=TOP ROWSPAN=9>

               <FONT FACE="Arial" SIZE=1>
               <B><U>January</U></B><BR>
               1 New Year's Day
               <BR><BR>
              <B><U>February</U></B><BR>
               14 Valentines Day<BR>
              17 President's Day
          <BR><BR>
               <B><U>March</U></B><BR>
               17 St. Patrick's Day
               <BR><BR>
               <B><U>April</U></B><BR>
               17 Passover<BR>
               18 Good Friday<BR>
               20 Easter Sunday
               <BR><BR>
               <B><U>May</U></B><BR>
               11 Mother's Day<BR>
               17 Armed Forces Day<BR>
               19 Victoria Day (Canada)<BR>
               26 Memorial Day Obsvd.
               <BR><BR>
               <B><U>June</U></B><BR>
            15 Father's Day<BR>
               </FONT></b>
      </TD>
      <TD VALIGN=TOP ROWSPAN=98>
            <FONT FACE="Arial" SIZE=1>

              <B><U>July</U></B><BR>
              4 Independance Day<BR>
              <BR>
              <B><U>September</U></B><BR>
              1  Labor Day<BR>
              27 Rosh Hashanah<BR>
              <BR>
              <B><U>October</U></B><BR>
              6 Yom Kippur<BR>
              13 Columbus Day Obsvd.<BR>
              13 Thanksgiving Day (Canada)<BR>
              24 United Nations Day<BR>
              31 Halloween
              <BR><BR>
              <B><U>November</U></B><BR>
              4  Election Day<BR>
              27 Thanksgiving Day<BR>
              <BR><BR>
              <B><U>December</U></B><BR>
              25 Christmas Day<BR>
               17 New Year's Eve<BR>
         </FONT>
      </TD>
    </TR>

<!---------------- STATUS ----------------------->

    <TR>
      <TD style="background-color: #99CCFF">
      <FONT FACE="Arial" SIZE=2><B>Employee Status</B></FONT>
      </TD>
      <TD style="background-color: #CCCCCC"><FONT="Arial">
      <FONT FACE="Arial" SIZE=2>FullTime
      <input type="checkbox" name="Full_Time" size="1" value="ON" <%=ChkFullTime%>>&nbsp;
      PartTime
      <input type="checkbox" name="Part_Time" size="1" value="ON" <%=ChkPartTime%>>&nbsp;&nbsp;
      </FONT>
     </TD>
    </TR>
<!----------- VACATION DAYS DUE ----------------->
<%=ErrMsg1%>
    <TR>

      <TD style="background-color: #99CCFF">
      <FONT FACE="Arial" SIZE=2><B># Vacation Days Due:</B></FONT>
      </TD>
      <TD style="background-color: #CCCCCC">

      <input type="text" name="Num_Due" size="3"value="<%=Request("Num_Due")%>">

      </TD>

    </TR>
<!--------------- HIRE DATE --------------------->
    <TR>
      <TD  style="background-color: #99CCFF">

      <FONT FACE="Arial" SIZE=2 COLOR="BLUE"><B><U><%=Rs3("FirstName")%></U>, Your Hire Date was:</B></FONT>
      </TD>
      <TD style="background-color: #CCCCCC">
      <FONT FACE="Arial" SIZE=2>
        <%=Rs3("DateHired")%>
        </FONT>
      </TD>
    </TR>
<!--------------- DATE RANGE -------------------->

    <TR>

        <TD style="background-color: #99CCFF" COLSPAN=2>
        <B>
        <FONT FACE="Arial" SIZE=2>Date range you are requesting to take off:<BR><BR></FONT>
        </TD>
      </TR>
<%=ErrMsg2%>
<%=ErrMsg3%>
      <TR>
      <TD VALIGN=TOP COLSPAN=2 style="background-color: #99CCFF">
      <FONT FACE="Arial" SIZE=2>
       Beginning date:
      <input type="text" name="BegDate" size="8"value="<%=Request("BegDate")%>">

      Ending date:
      <input type="text" name="EndingDate" size="8"value="<%=Request("EndingDate")%>">

        </TD>
    </TR>

<!--------------- COMMENTS --------------------->
    <TR>
      <TD COLSPAN=2 font="Verdona" style="background-color: #99CCFF">
      <FONT FACE="Arial" SIZE=2><B>Comments:</B></FONT>
      </TD>
    </TR>
    <TR>
        <TD COLSPAN=2 style="background-color: #CCCCCC">

        <input type="text" name="Comments" size="76" value="<%=Request("Comments")%>">
      </TD>

    </TR>
<!----------------- SUBMIT --------------------->
    <TR>
      <TD  COLSPAN=2 style="background-color: #CCCCCC">
        <p align="center" SIZE=2><input type="submit" onsubmit="ValidDataCheck" value="Submit for Approval" name="B1"></p>
      </TD>
    </TR>
  </TABLE>
  </form>

<p>&nbsp;</p>
</center>
</BODY>

</html>
Avatar of gladxml
gladxml

dba123,

As I see your code your mixing client side and serve side scripting...

<input type="submit" onsubmit="ValidDataCheck" value="Submit for Approval" name="B1">

Also the onsubmit will not have any function at all since in your code there is no client side scripting.. as I see it all are server side...

Now try to use this line

<input type="submit" value="Submit for Approval" name="B1">


The on the top part of the serve side script... try to add the if request.form("B1") = "Submit for Approval" then on the top just liek on the below code...

========
<%
if request.form("B1") = "Submit for Approval" then


If EmployeeID = 0 Then
  Response.Redirect("not_auth.html")
End If

==========

Then an end if on the end... just like below

=====
  If Not Rs.EOF Then SaveReqData = Rs(0)

End Function

end if

====


AS you can see when you run the code with the if then in it, on the first loading it will check for the instance of the request.form("B1")... SInce on the first loading you does not any pass any request therefore it will disregard the asp code... Then when you hit submit it will execute the asp code...

I also assume that the page is being pass to itself there fore make sure that wherever the file of this page is also form action of the page...

assuming if the filename is new_vacation_entry.asp

then on your form it mus also be

<form method="POST" action="new_vacation_entry.asp" name=form1>

HTH...

HAppy programming...
ASKER CERTIFIED SOLUTION
Avatar of ActiveMedia
ActiveMedia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

Thanks, you cleared that up.  I ended up changing this:
If Invalid = 1 Then
    ValidDataCheck = 0
 Else
    ValidDataCheck = 1
 End If

To This:

If Invalid1 = 1 or Invalid2 = 1 or Invalid3 = 1 Then
      ValidDataCheck = 0
   Else
      ValidDataCheck = 1
   End If

There was a couple of items I didn't understand:

1) ValidDataCheck = not ( invalid1 or invalid2 )

2)  When you code it like you said:  If Request("submitted") = "1" Then
 If ValidDataCheck() Then

What is it resolving to when it checks the function...what the function bring back?  I am not sure of the ValidDataCheck() part, what value does it return for the if statment to evaluate...how does this work?

3) What did you mean by use #2 at the beginning of my listing...listing meaning my table that is written to the screen or part of my function?

Thanks for clearing this up.  Please answer 1, 2, and 3 if possible, it would be a great help to clear up what you are trying to tell me but otherwise, it works!!
Explanation of:
    ValidDataCheck = not ( invalid1 or invalid2 )

If invalid1 is a Boolean variable it will always be either TRUE or FALSE.  It's OK to 1 and 0 as you did but essentially it is a Boolean situation, it's either yes or no, up or down, etc.    Similarly for invalid2.

The expression: "invalid1 OR invalid2 " means that the result will be TRUE if either invalid1 is TRUE or if invalid2 is TRUE or if both of them are TRUE.

The expression: "not (invalid1 or invalid2)" simply inverts the previous result.

Thus the overall expression:
"ValidDataCheck = not (invalid1 or invalid2)"
means that ValidDataCheck will be set to FALSE if either (or both) invalid1 or invalid2 are TRUE.

It's also OK to do it over several lines with an IF ... THEN ... ELSE ... END IF approach if you prefer.



Explanation of:
    If ValidDataCheck() Then

In each function that you define (usually near the end of the function) the name of the function is repeated followed by an "=" sign and the result that you want to be passed back to the calling program.   In this particular case ValidDataCheck takes on the value that indicates whether the two dates are both valid or not.


Comment on:
    "3) What did you mean by use #2 at the beginning of my listing"

By "listing" I meant the program code that you displayed with your question.  Basically it was just a reminder to put the parentheses () after the function name so that there is no confusion between a variable (which already stores a particular value eg 450) and a function (which is a sub-program that contains a set of steps that work out the result.

HTH

BTW Thanks for the appreciation and the points.