Processing Forms with Functions

In the last installment, I discussed how you can write functions in VBScript and PHP to help you generate multiple instances of the same form. I will now discuss how to write a function to process those forms. In the next entry, I’ll show you how to display those results once you’ve processed the form.

In this entry, I will be assuming that you are using a database to store your information, but you don’t necessarily have to do that. You can certainly use the same logic to write functions to send an e-mail message instead of storing the information in a database. I will also be assuming that you already know how to connect to a database in your given language.


A few items I should discuss before diving into this. In the examples below, I will be using a function called “openCon” to create and open a connection to my database called “myCon”. In addition, inside the function that opens my connection, I have set the cursortype and locktype of my recordset, which is why I set my recordset before opening my connection. I’ve set my locktype to 3, which is equivalent to adLockOptimistic. I have set my cursortype to 1, which is equivalent to adOpenKeyset. Finally, I have set a variable called “maintable”, which contains the name of the table I will be manipulating with this function. The variable “idname” contains the name of me primary key in that table. This makes the function more flexible, as you can use it to process different forms as long as you set the “maintable” and “idname” variables correctly with each form.

Before running the function below, I have also defined an array called “reqdArr” which contains the names of all of my “required” items in my form. That is useful for form validation, to make sure that all of your “required” fields contain some sort of value. I am then using two scripting dictionaries to generate useful error messages. The first dictionary includes all of my form labels (which I discussed in my previous entry). The second dictionary is empty until I come across an error. When an error occurs, I add an item to that dictionary that will be returned as the text of my error messages.

I will explore some helpful functions in future installments that are referenced in the code below, as well. Those functions include “myRegExTest”, which basically checks a string to see if a specific string exists within. I will also be using a function called “txtPhoneValidate”, which processes a phone number and removes all of the extra characters like parentheses and hyphens. That way, just the digits of the phone number are entered into the database so that you can manipulate the phone numbers later and return them in any format you choose.

To begin, let’s see an example of a function that will process the form we generated in the last installment. I will be writing my examples in VBScript, but as I mentioned in the previous entry, I can just as easily show you examples in PHP if you need me to do so.

The best way to process a form that’s dumping into a database in VBScript is to do the following:
<%
function processForm(myID)
For Each myKey in reqdArr
If(Trim(Request.Form(myKey)) = "") Then
myErrors.Add "" & myKey & "","" & formLabels(myKey) & ""
End If
Next

If(myErrors.Count <> 0) Then
%>
<div class="dbmessage">
There was an error processing the form. Following is a list of the required information that was not provided and the errors that occurred.
</div>
<ul>
<%
For Each myKey in myErrors.Keys
%>
<li>
<%=myErrors(myKey)%>
</li>
<%
Next
%>
</ul>
<div class="dbmessage">
Please go back and try again.
</div>
<%
Else
Set myrs = Server.CreateObject("ADODB.RecordSet")
openCon
myrs.activeconnection = myCon

if(myID = 0) Then
strSQL = maintable
Else
strSQL = "SELECT * FROM `" & maintable & "` WHERE `" & idname & "` = " & myID
End If

myrs.source = strSQL
myrs.open

If(myID = 0) Then
myrs.AddNew
End If

for each myKey in myrs.Fields
If(myRegExTest(myKey,"phone")) OR (myRegExTest(myKey,"fax")) Then
myrs.Fields(myKey.Name).Value = txtPhoneValidate(Trim(Request.Form(myKey.Name)))
ElseIf Not (myKey.Name = idname) Then
myrs.Fields(myKey.Name).Value = Trim(Request.Form(myKey.Name)))
End If
next
rs.Update
If myCon.Errors.Count > 0 Then
For Each Err In myCon.Errors
Response.Write("Error " & Err.SQLState & ": " & _
Err.Description & " | " & Err.NativeError)
Next
myCon.Errors.Clear
rsResults.CancelUpdate
failed = 1
End If
End If
If(failed <> 1) Then
myrs.Close
If myID = 0 Then
myrs.Open "SELECT MAX(" & idname & ") FROM " & maintable, myCon
myID = myrs(0)
End If
Response.Write(showResults(myID))
End If
End Function
%>

In the example above, I am assuming that if “myID” is equal to 0, then it must be a new form. If the ID is not equal to 0, then I am most likely processing an existing entry that simply needs to be edited rather than added to the database.

Tech Tags: HTMLCenter

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

No Responses

Post Your Comment

Your email address will not be published.