View Single Post
Old 10-02-2003, 02:51 PM   #4 (permalink)
rubicon
Stop. Think. Question.
 
rubicon's Avatar
 
Location: Redondo Beach, CA
I have included sample VBScript. I pulled this from an existing page so it's out of context but it may work as is.

The VBScript checks to see if the form has already been submitted. If not, it presents the form with empty fields.

If it has been submitted, does it contain field validation errors? If so, display the field values and an error message.

When all errors are corrected the form is submitted via email. You can modify the code to create a text file on the server, add a database record, etc.

This is all server-side code and requires an IIS server (Windows 2000, 2003, XP) to run.

This code includes some string routines which are also included below.

PHP Code:
<!-- #include virtual="/includes/string_routines.asp" -->

<%

Dim strFieldMissing(10)

' see string_routins.asp include above
strErrorMsg = "<span class=formerror>&nbsp;&nbsp;<-- Required</span>"
strEmailErrorMsg = "<span class=formerror>&nbsp;&nbsp;<-- Doesn'
t match</span>"
strFirstName = makeAlpha(Request.Form("
firstname"))
strLastName = makeAlpha(Request.Form("
lastname"))
strTitle = makeAlpha(Request.Form("
title"))
strEmail1 = Request.Form("
email1")
strEmail2 = Request.Form("
email2")
strCompany = makeAlpha(Request.Form("
company"))
strStreet1 = makeAlpha(Request.Form("
street1"))
strStreet2 = makeAlpha(Request.Form("
street2"))
strCity = makeAlpha(Request.Form("
city"))
strState = makeAlpha(UCase(Request.Form("
state")))
strZip = Request.Form("
zip")
strTelephone = Request.Form("
telephone")
strReferrer = Request.Form("
referrer")
strComments = stringClean(Request.Form("
comments"))
strTOSAgree = Request.Form("
tos_agree")

for i = 0 to 10
    strFieldMissing (i) = false
next

if strFirstName = "" then strFieldMissing(0) = true
if strLastName = "" then strFieldMissing(1) = true
if not isEmail(strEmail1) then strFieldMissing(2) = true
if not isEmail(strEmail2) then strFieldMissing(3) = true
if strCompany = "" then strFieldMissing(4) = true
if strStreet1 = "" then strFieldMissing(5) = true
if strCity = "" then strFieldMissing(6) = true
if strState = "" or Len(strState) < 2 then strFieldMissing(7) = true
if not isZip(strZip) then strFieldMissing(8) = true
if not isPhone(strTelephone) then strFieldMissing(9) = true
if strTOSAgree <> "
true" then
    strFieldMissing(10) = true
    strTOSAgreeState = ""
else
    strTOSAgreeState = "
checked"
end if

if strEmail1 = strEmail2 then
    strEmailMatch = true
else
    strEmailMatch = false
end if

' do we have any erros?
strFormHasErrors = false
for i = 0 to 10
    if strFieldMissing (i) = true then strFormHasErrors = true
next

' have we run this before?
checkForSubmit = Request.Form("
wcp_submit")
if checkForSubmit <> "
Submit" then strFirstSubmit = true

' only display the form if this is the first time or we have errors to correct
if strFirstSubmit or strFormHasErrors then
%>

blah blah

<%
    if not strFirstSubmit and strFormHasErrors then Response.Write ("
<class=formerror align=center><B>Your Form Has Errors Please Correct and Submit Again</B></p>")
%>
    <form name="
wcp_app" method="POST" action="default.asp">
    <table align="
center">
        <tr>
            <td valign="
middle">First Name*</td>
            <td valign="
middle"><input name="firstname" type="text" size="30" maxlength="80" value="<%=strFirstName%>">
                <% if not strFirstSubmit and strFieldMissing(0) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr>
            <td valign="
middle">Last Name*</td>
            <td valign="
middle"><input name="lastname" type="text" size="30" maxlength="80" value="<%=strLastName%>">
                <% if not strFirstSubmit and strFieldMissing(1) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr>
            <td valign="
middle">Title</td>
            <td valign="
middle"><input name="title" type="text" size="30" maxlength="80" value="<%=strTitle%>">
            </td>
        </tr>
        <tr>
            <td valign="
middle">E-mail*</td>
            <td valign="
middle"><input name="email1" type="text" size="30" maxlength="80" value="<%=strEmail1%>">
                <%
                if not strFirstSubmit and not strEmailMatch then
                    Response.Write (strEmailErrorMsg)
                else
                    if not strFirstSubmit and strFieldMissing(2) then Response.Write (strErrorMsg)
                end if
                %>
            </td>
        </tr>
        <tr>
            <td valign="
middle">E-mail (confirm)*</td>
            <td valign="
middle"><input name="email2" type="text" size="30" maxlength="80" value="<%=strEmail2%>">
                <%
                if not strFirstSubmit and not strEmailMatch then
                    Response.Write (strEmailErrorMsg)
                else
                    if not strFirstSubmit and strFieldMissing(3) then Response.Write (strErrorMsg)
                end if
                %>
            </td>
        </tr>
        <tr> 
            <td valign="
middle">Company Name*</td>
            <td valign="
middle"><input name="company" type="text" size="30" maxlength="80" value="<%=strCompany%>">
                <% if not strFirstSubmit and strFieldMissing(4) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr>
            <td valign="
middle">Address 1*</td>
            <td valign="
middle"><input name="street1" type="text" size="30" maxlength="80" value="<%=strStreet1%>">
                <% if not strFirstSubmit and strFieldMissing(5) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr> 
            <td valign="
middle">Address 2</td>
            <td valign="
middle"><input name="street2" type="text" size="30" maxlength="80" value="<%=strStreet2%>"></td>
        </tr>
        <tr> 
            <td valign="
middle">City*</td>
            <td valign="
middle"><input name="city" type="text" size="30" maxlength="80" value="<%=strCity%>">
                <% if not strFirstSubmit and strFieldMissing(6) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr> 
            <td valign="
middle">State*</td>
            <td valign="
middle">
                <input name="
state" type="text" size="2" maxlength="2" value="<%=strState%>">
                <% if not strFirstSubmit and strFieldMissing(7) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr> 
            <td valign="
middle">ZIP*</td>
            <td valign="
middle"><input name="zip" type="text" size="10" maxlength="10" value="<%=strZip%>">
                <% if not strFirstSubmit and strFieldMissing(8) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr>
            <td valign="
middle">Telephone*</td>
            <td valign="
middle"><input name="telephone" type="text" size="12" maxlength="12" value="<%=strTelephone%>">
                <% if not strFirstSubmit and strFieldMissing(9) then Response.Write (strErrorMsg) end if %>
            </td>
        </tr>
        <tr>
            <td valign="
middle">How Did You Learn<br>About Us?</td>
            <td valign="
middle">
                <select name="
referrer">
                    <option value="
no answer" selected>-- Select from list --</option>
                    <option value="
Customer Referral">Customer Referral</option>
                    <option value="
E-mail">E-mail</option>
                    <option value="
Internet Advertising">Internet Advertising</option>
                    <option value="
Link from Another Web Site">Link from Another Web Site</option>
                    <option value="
On-Line Seminar">Attended On-Line Seminar</option>
                    <option value="
Printed Advertising">Printerd Advertising</option>
                    <option value="
Sales Call">Sales Call</option>
                    <option value="
Search Engine">Search Engine</option>
                    <option value="
Trade Journal">Trade Journal</option>
                    <option value="
Trade Show">Trade Show</option>
                    <option value="
Yellow Pages">Yellow Pages</option>
                    <option value="
Other">Other</option>
                </select>
            </td>
        </tr>
        <tr>
            <td valign="
middle">Comments</td>
            <td valign="
middle"><textarea name="comments" cols="30" rows="5"><%=strComments%></textarea></td>
        </tr>
        <tr>
            <td align="
center" colspan="2">
                <textarea readonly rows="
10" cols="50"><!-- #include virtual="/web_conferencing/tos.txt" --></textarea>
            </td>
        </tr>
        <tr>
            <td align="
center" colspan="2">
                <br>
                <input name="
tos_agree" type="checkbox" value="true" <%=strTOSAgreeState%>> I AGREE TO THESE TERMS OF SERVICE
                <% if not strFirstSubmit and strFieldMissing(10) then Response.Write (strErrorMsg) end if %>
                <br><br>
                <input type="
submit" name="wcp_submit" value="Submit">
            </td>
        </tr>
    </table>
    </form>
    <p><a name="
rates">*</a>Phone and web conference usage is billed at the prevailing rate; click <a href="/web_conferencing/web_conf.asp#plans">here</a> to review Plan and Rate information.</p>
<%
' we've submitted with no errorsso process the form data
else
%>
<%
    
Set Mailer Server.CreateObject ("SMTPsvg.Mailer")
    
    
Mailer.FromName "auto-mailer"
    
Mailer.FromAddress "p@l.com"
    
Mailer.RemoteHost "mail.verio.com"
    
Mailer.Subject "subject"
    
Mailer.AddRecipient "p@l.com""p@l.com"
    
strBodyText "Information" vbCrLf vbCrLf _
        
"Referrer: General Sign-Up Page" vbCrLf _
        
"Name: " strFirstName " " strLastName vbCrLf _
        
"Title: " strTitle vbCrLf _
        
"Company: " strCompany vbCrLf _
        
"Street1: " strStreet1 vbCrLf _
        
"Street2: " strStreet2 vbCrLf _
        
"City: " strCity vbCrLf _
        
"State: " strState vbCrLf _
        
"ZIP: " strZip vbCrLf _
        
"Telephone: " strTelephone vbCrLf _
        
"E-mail: " strEmail1 vbCrLf _
        
"Referral: " strReferrer vbCrLf _        
        
"Comments: " vbCrLf strComments vbCrLf _
        
"TOS Agreement: " strTOSAgreeState vbCrLf
    Mailer
.BodyText strBodyText

    On Error Resume Next
    returnCode 
Mailer.SendMail
    
if returnCode TRUE then
        Response
.Write "<p>Thank you.  Your account request has been sent and you will receive an e-mail confirmation when it has been processed.</p>"
    
else
        
strManualEmail "mailto:p@l.com?from=" strEmail "&subject=request" "&body=" strBodyText
        Response
.Write "<p>The system was unable to send your account request due to a mail send failure (Error: " Mailer.Response ").</p>"
%>
        <
p>Please <a href="/contact/">contact us</ato speak with a representative.</p>
<%
    
end if
    
Set Mailer=Nothing
end 
if
%> 
string_routines.asp

PHP Code:
<%

' REGULAR EXPRESSIONS  
Source: [url]http://www.aspalliance.com/chrisg/[/url]

Function makeAlpha(text)  
    
makeAlpha=regexReplace(text"[^A-Za-z0-9]","")  
End Function  
 
Function 
stringClean(text)  
    
stringClean=regexReplace(text"[^\w\d\s\@\_\-\.\n\r\/]","")  
End Function  
 
Function 
isEmail(email)  
    
isEmail=regexTest("[A-Za-z0-9\_\-]+\@[A-Za-z0-9\_\-]+.*\.\w{2,3}",email)  
End Function  

Function 
isZip(text)
    
isZip=regexTest("^\d{5}(-\d{4})?$",text)
End Function

Function 
isPhone(text)
    
isPhone=regexTest("^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$",text)
End Function

Function 
regexReplace(sourceexpressionreplaceWith)  
    
dim regEx ' Create variables.  
    Set regEx = New RegExp ' 
Create regular expression.  
    
regEx.Pattern expression ' Set pattern.  
    regEx.IgnoreCase = True ' 
Make case insensitive.  
    
regEx.Global = True ' Make global  
    regexReplace = regEx.Replace(source, replaceWith) ' 
Make replacement.  
End Function  
 
Function 
regexTest(expressiontext)  
    
dim regExMatchMatches ' Create variable.  
    Set regEx = New RegExp ' 
Create a regular expression.  
    
regEx.Pattern expression ' Set pattern.  
    regEx.IgnoreCase = True ' 
Set case insensitivity.  
    
regEx.Global = True ' Set global applicability.  
    regexTest = regEx.test(text) ' 
Execute search.  
End Function  
 
Function 
regexMatch(expressiontext)
    
dim regExMatchMatches ' Create variable.  
    Set regEx = New RegExp ' 
Create a regular expression.  
    
regEx.Pattern expression ' Set pattern.  
    regEx.IgnoreCase = True ' 
Set case insensitivity.  
    
regEx.Global = True ' Set global applicability.  
    Set Matches = regEx.Execute(text) ' 
Execute search.  
    for 
each Match in Matches ' Iterate Matches collection.  
        RetStr = RetStr & Match.Value & ","  
    next  
    regexMatch = RetStr  
End Function  

%> 
__________________
How you do anything is how you do everything.
rubicon is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73