Following on from my ASP issues....
Discussion
Right so ignoring other small issues with my scripting this is the script I have so far for my web form, which writes to a database when I press submit and displays a message on the screen thanking the user. If the fields aren't filled in properly it returns the user to the for, highlights the fields and presents an error message...
At least it would... but I don't know how to write the piece of script to order it to validate the form -> write to database/return to form -> present message one way or the other.
I'm shite with google as well so any pointers would be muchos appreciated.
Oh, I know I used Addnew and function/subfunction when I was warned off them, I had to as the guy I'm working with who has written a lot of other script on the site kicked off at me for trying to use Insert method. I think it is just what he is used to.
Thanks
<HTML>
<title>Migration</title>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="NICHOLAS JONES">
<BODY>
<%
If request("Submit"
<> "" Then
'Process Form (called ValidateField)
ValidateField()
Else
DisplayForm()
End If
%>
<hr>
<%
response.write request("Submit"
%>
</BODY>
</HTML>
<%
Sub DisplayForm()
%>
<table>
<form action="stk_migrate.asp" method="post">
<tr><td colspan="2">Please enter your details here:</td>
</tr>
<tr>
<td>Full Name:</td>
<td><input name="strname"></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input name="stremailadd"></td>
</tr>
<tr>
<td>XP Login Name:</td>
<td><input name="strxplogin"></td>
</tr>
<tr>
<td>Department Name:</td>
<td><input name="strdeptname"></td>
</tr>
<tr>
<td>Ready For Migration?</td>
<td><select class=black11 name="strreadymigrate" style="width:60">
<option value="Blank">
<option value="1">Yes
<option value="0">No
</select></td>
</tr>
<tr>
<td valign=top>Comments</td>
<td><textarea class=black11 name="strcomment" ROWS="3" COLS="50">
</textarea></td>
</tr>
</table>
<br /><br />
<input type="Submit" name="Submit" value="Submit">
<input type="Reset" value="Reset">
</form>
<%
End Sub
Function ValidateField(sFieldValue, sFieldType)
Dim bFieldIsOkay
' defaut it to true
bFieldIsOkay = True
' go to the field name to validate the entry
Select Case LCase(sFieldType)
Case "strname"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "stremailadd"
If Len(sFieldValue) < 5 Then bFieldIsOkay = False
Else
If Instr(1, sFieldValue, " "
<> 0 Then bFieldIsOkay = False
Else
If InStr(1, sFieldValue, "@"
< 2 Then bFieldIsOkay = False
Else
If InStrRev(sFieldValue, "."
< InStr(1, sFieldValue, "@"
+ 2 Then bFieldIsOkay = False
End If
End If
End If
End If
Case "strxplogin"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "strdeptname"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "strreadymigrate"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
End If
Case Else 'if an unknown type gets in reject form!
bFieldIsOkay = False
End Select
ValidateField = bFieldIsOkay
End Function
Sub ShowFormField(strField)
' This function needs access to the dictFields object!
%>
<tr>
<td ALIGN="right"><B><%= strField %>:</B> </TD>
<td><INPUT NAME="<%= strField %>" TYPE="text" VALUE="<%= Request.Form(strField) %>"></INPUT></TD>
<td><%
If dictFields(LCase(strField)) Then
Response.Write "<IMG SRC=""query.gif"" BORDER=""0"" WIDTH=""25"" HEIGHT=""25"">"
End If
%></td>
</tr>
<%
End Sub
%>
<%
Dim Field 'looping variable
Dim dictFields 'dictionary for failed fields
Set dictFields = Server.CreateObject("Scripting.Dictionary"
For Each Field in Request.Form
If ValidateField(Request.Form(Field), Field) = False Then
dictFields.Add LCase(Field), True
End If
Next
If Request.Form.Count <> 0 And dictFields.Count = 0 Then
%>
<B>Your Form has been submitted successfully Thank you!</B><BR>
<B>Here's what you entered, if there are any problems please contact an administrator:</B><BR>
<%
' Loop through the fields writing out the field name and value.
For Each Field In Request.Form
Response.Write Field & ": " & Request.Form(Field) & "<BR>" & vbCrLf
Next 'Field
Else
If Request.Form.Count <> 0 Then
%>
I'm sorry but your form wasn't filled out correctly.
Please correct the fields indicated by the check marks.
<%
End If
<%
Function WriteToDB
'declare variables
Dim objConn, strConn, objRS, strSQL, strOut
Dim strname, stremailadd, strxplogin, strdeptname, strreadymigrate, strcomment
'Set values into variables
'create connection object
Set objConn = Server.CreateObject("ADODB.Connection"
objConn.Open "DRIVER={Sql Server};SERVER=AUK52171;database=NICK_TEST;UID=appsuser;PWD=appsuser"
%>
<%
'create recordset object
Set objRS = Server.CreateObject("ADODB.Recordset"
'create SQL string for inserting data into database
strSQL = "SELECT * FROM stk_migrate"
objRS.ActiveConnection = objConn
objRS.LockType = 3
objRS.Open "stk_migrate", objConn, , , adCmdTable
'now add data with AddNew Method
objRS.AddNew
objRS("name"
= strname
objRS("emailadd"
= stremailadd
objRS("xplogin"
= strxplogin
objRS("deptname"
= strdeptname
objRS("readymigrate"
= strreadymigrate
'update the record
objRS.Update
'close the recordset and destroy from memory
objRS.Close
Set objRS = Nothing
'close connection object
objConn.Close
Set objConn = Nothing
End Function
%>
Grrr damn smileys
At least it would... but I don't know how to write the piece of script to order it to validate the form -> write to database/return to form -> present message one way or the other.
I'm shite with google as well so any pointers would be muchos appreciated.
Oh, I know I used Addnew and function/subfunction when I was warned off them, I had to as the guy I'm working with who has written a lot of other script on the site kicked off at me for trying to use Insert method. I think it is just what he is used to.

Thanks
<HTML>
<title>Migration</title>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="NICHOLAS JONES">
<BODY>
<%
If request("Submit"
<> "" Then 'Process Form (called ValidateField)
ValidateField()
Else
DisplayForm()
End If
%>
<hr>
<%
response.write request("Submit"

%>
</BODY>
</HTML>
<%
Sub DisplayForm()
%>
<table>
<form action="stk_migrate.asp" method="post">
<tr><td colspan="2">Please enter your details here:</td>
</tr>
<tr>
<td>Full Name:</td>
<td><input name="strname"></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input name="stremailadd"></td>
</tr>
<tr>
<td>XP Login Name:</td>
<td><input name="strxplogin"></td>
</tr>
<tr>
<td>Department Name:</td>
<td><input name="strdeptname"></td>
</tr>
<tr>
<td>Ready For Migration?</td>
<td><select class=black11 name="strreadymigrate" style="width:60">
<option value="Blank">
<option value="1">Yes
<option value="0">No
</select></td>
</tr>
<tr>
<td valign=top>Comments</td>
<td><textarea class=black11 name="strcomment" ROWS="3" COLS="50">
</textarea></td>
</tr>
</table>
<br /><br />
<input type="Submit" name="Submit" value="Submit">
<input type="Reset" value="Reset">
</form>
<%
End Sub
Function ValidateField(sFieldValue, sFieldType)
Dim bFieldIsOkay
' defaut it to true
bFieldIsOkay = True
' go to the field name to validate the entry
Select Case LCase(sFieldType)
Case "strname"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "stremailadd"
If Len(sFieldValue) < 5 Then bFieldIsOkay = False
Else
If Instr(1, sFieldValue, " "
<> 0 Then bFieldIsOkay = FalseElse
If InStr(1, sFieldValue, "@"
< 2 Then bFieldIsOkay = FalseElse
If InStrRev(sFieldValue, "."
< InStr(1, sFieldValue, "@"
+ 2 Then bFieldIsOkay = FalseEnd If
End If
End If
End If
Case "strxplogin"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "strdeptname"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
Case "strreadymigrate"
If Len(sFieldValue) = 0 Then bFieldIsOkay = False
End If
Case Else 'if an unknown type gets in reject form!
bFieldIsOkay = False
End Select
ValidateField = bFieldIsOkay
End Function
Sub ShowFormField(strField)
' This function needs access to the dictFields object!
%>
<tr>
<td ALIGN="right"><B><%= strField %>:</B> </TD>
<td><INPUT NAME="<%= strField %>" TYPE="text" VALUE="<%= Request.Form(strField) %>"></INPUT></TD>
<td><%
If dictFields(LCase(strField)) Then
Response.Write "<IMG SRC=""query.gif"" BORDER=""0"" WIDTH=""25"" HEIGHT=""25"">"
End If
%></td>
</tr>
<%
End Sub
%>
<%
Dim Field 'looping variable
Dim dictFields 'dictionary for failed fields
Set dictFields = Server.CreateObject("Scripting.Dictionary"

For Each Field in Request.Form
If ValidateField(Request.Form(Field), Field) = False Then
dictFields.Add LCase(Field), True
End If
Next
If Request.Form.Count <> 0 And dictFields.Count = 0 Then
%>
<B>Your Form has been submitted successfully Thank you!</B><BR>
<B>Here's what you entered, if there are any problems please contact an administrator:</B><BR>
<%
' Loop through the fields writing out the field name and value.
For Each Field In Request.Form
Response.Write Field & ": " & Request.Form(Field) & "<BR>" & vbCrLf
Next 'Field
Else
If Request.Form.Count <> 0 Then
%>
I'm sorry but your form wasn't filled out correctly.
Please correct the fields indicated by the check marks.
<%
End If
<%
Function WriteToDB
'declare variables
Dim objConn, strConn, objRS, strSQL, strOut
Dim strname, stremailadd, strxplogin, strdeptname, strreadymigrate, strcomment
'Set values into variables
'create connection object
Set objConn = Server.CreateObject("ADODB.Connection"

objConn.Open "DRIVER={Sql Server};SERVER=AUK52171;database=NICK_TEST;UID=appsuser;PWD=appsuser"
%>
<%
'create recordset object
Set objRS = Server.CreateObject("ADODB.Recordset"

'create SQL string for inserting data into database
strSQL = "SELECT * FROM stk_migrate"
objRS.ActiveConnection = objConn
objRS.LockType = 3
objRS.Open "stk_migrate", objConn, , , adCmdTable
'now add data with AddNew Method
objRS.AddNew
objRS("name"
= strnameobjRS("emailadd"
= stremailadd objRS("xplogin"
= strxplogin objRS("deptname"
= strdeptname objRS("readymigrate"
= strreadymigrate'update the record
objRS.Update
'close the recordset and destroy from memory
objRS.Close
Set objRS = Nothing
'close connection object
objConn.Close
Set objConn = Nothing
End Function
%>
Grrr damn smileys

Edited by Nic Jones on Monday 22 October 19:33
If the other guy is kicking off at you then I suggest he does it as however you do it it'll be wrong.
You can choose between client side validation or server side validation, or both.
To help you with your googling look at this lot: http://www.google.co.uk/search?hl=en&q=asp+for...
You can choose between client side validation or server side validation, or both.
To help you with your googling look at this lot: http://www.google.co.uk/search?hl=en&q=asp+for...
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


