Solved

Classic ASP. Validate data in a form and post the result to a URL.

Posted on 2006-11-16
10
781 Views
Last Modified: 2008-01-09
Hi,

I'm a complete novice when it comes to classic asp and was wondering if anyone could show me how to create a form which collects a CustomerID, Email Address and Password then validates this data in a database to check that the customerID is valid. If valid, the information will be posted to a url, otherwise we'll re-direct to an error page.

I come from an event driven programming background and have already wrapped the database validation code in a function, so I'm really just looking for a way to control the program flow by passing the CustomerID to the function and acting depending on the result.

I could do it in VB.Net without a problem but Classic ASP is a steep learning curve and time is tight.

Thanks.

Jay.
0
Comment
Question by:jay1971a
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:deathtospam
Comment Utility
jay1971a --

When you say that you want to make sure the "CustomerID is valid", what does that mean?  Are you checking only to make sure that the CustomerID the user enters already exists?  Or is there something else I'm missing?


-= DeathToSpam =-
0
 

Author Comment

by:jay1971a
Comment Utility
Hi,

I just need to check that the customerID exists in the customers table and that it is active.

Thanks.
0
 
LVL 6

Expert Comment

by:iscode
Comment Utility
0
 
LVL 9

Expert Comment

by:deathtospam
Comment Utility
jay1971a --

What kind of database are you connecting to (i.e. SQL Server, Access), and what version (e.g. 2005, 2003, 2005)?


-= DeathToSpam =-
0
 
LVL 13

Expert Comment

by:samic400
Comment Utility
let's say you have a form that has an input field called 'custid'. once the form get's submitted and you could have an ASP script that has the following code below. In the script you would do the following:

<%
dim mConn
dim mRS

set mConn = server.createobject("ADODB.CONNECTION")
mConn.open "connection string to whatever - SQL Server, etc."

set mRS = server.createobject("ADODB.RECORDSET")
mRS.Open "select * from custtable where custid=" & request.form("custid") & "'", mConn

if not mRS.EOF then
      response.write "Found the ID"
 else
     response.write "ID does not exist in DB"
end if

mRS.Close
set mRS = Nothing

mConn.Close
set mConn = Nothing

%>
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 13

Expert Comment

by:samic400
Comment Utility

form.html to take in the custid

<html>
<body>
<form action='process.asp' method='post'>
<input type='text' name='custid' />
<input type='submit' />
</form>
</body>
</html>

NOTE - the input field is called CUSTID

process.asp is below -

<html>
<body>
<%
dim mConn
dim mRS

set mConn = server.createobject("ADODB.CONNECTION")
mConn.open "connection string to whatever - SQL Server, etc."

set mRS = server.createobject("ADODB.RECORDSET")
mRS.Open "select * from custtable where custid=" & request.form("custid") & "'", mConn

if not mRS.EOF then
      response.write "Found the ID"
 else
     response.write "ID does not exist in DB"
end if

mRS.Close
set mRS = Nothing

mConn.Close
set mConn = Nothing

%>
</body>
</html>
0
 
LVL 9

Expert Comment

by:deathtospam
Comment Utility
jay1971a --

Try this code out.  You'll only need to change two lines of code to get it to work:

1) Modify the value of the DATABASE_CONNECTION_STRING constant so that it points to your database.
2) Modify the value of sSqlQuery variable so that it checks your table to see if the Customer ID exists.

======================================================
<%      Option Explicit
      Response.Buffer = True

      Dim bIsPostback, sCustomerID, sEmailAddress, sPassword, sDisplayMessageHTML
      Dim sSqlQuery, oConnection, oRecordset, bCustomerIdExists, sRedirectURL
      
      '// NOTE: You need to enter this information to connect to your database.
      Const DATABASE_CONNECTION_STRING = "Driver={SQL Server};Server=SERVERNAME;Database=DATAB
ASENAME;Uid=USERNAME;Pwd=PASSWORD;

      bIsPostback = (Trim(Request.Form("IsPostback")) <> "")
      sDisplayMessageHTML = ""

      If (NOT bIsPostback) Then
            '// This is the user's first time visiting the page.
            sCustomerID = "":  sEmailAddress = "":  sPassword = ""
      Else
            '// User has been to the page before.
            sCustomerID = Trim(Request.Form("CustomerID"))
            sEmailAddress = Trim(Request.Form("EmailAddress"))
            sPassword = Trim(Request.Form("Password"))

            If (sCustomerID = "") Then
                  '// ERROR: Customer ID is blank.
                  sDisplayMessageHTML = "Please enter the value for the <b>Customer ID</b> field."
            ElseIf NOT IsNumeric(sCustomerID) Then
                  '// ERROR: Customer ID was specified, but is not a valid number.
                  sDisplayMessageHTML = "Please enter a valid number for the <b>Customer ID</b> field."
            ElseIf (sEmailAddress = "") Then
                  '// ERROR: Email Address is blank.
                  sDisplayMessageHTML = "Please enter the value for the <b>Email Address</b> field."
            ElseIf (sPassword = "") Then
                  '// ERROR: Password is blank.
                  sDisplayMessageHTML = "Please enter the value for the <b>Password</b> field."
            Else
                  '// Check to see if the specified customer ID exists in the database.
                  '// NOTE: You need to change "TableName" and "CustomerID" to reflect your table's info.
                  'sSqlQuery = "SELECT 'True' AS [CustomerIdExists] FROM TableName WHERE CustomerID = " & sCustomerID
                  sSqlQuery = "SELECT 'True' AS [CustomerIdExists] FROM client_batches WHERE client_id = " & sCustomerID

                  Set oConnection = Server.CreateObject("ADODB.Connection")
                  Call oConnection.Open(DATABASE_CONNECTION_STRING)
                  Set oRecordset = oConnection.Execute(sSqlQuery)
                        bCustomerIdExists = (NOT oRecordset.EOF)
                  Set oRecordset = Nothing
                  Call oConnection.Close():  Set oConnection = Nothing

                  If NOT bCustomerIdExists Then
                        '// ERROR: Customer ID is a valid number, but could not be found in the database.
                        sDisplayMessageHTML = "The customer ID you specified could not be found."
                  Else
                        '// Here is where you create the URL you want to redirect to.
                        sRedirectURL = "http://www.somewhere.com/somepage.asp?customerID=" & Server.URLEncode(sCustomerID)
                        sRedirectURL = sRedirectURL & "&emailaddress=" & Server.URLEncode(sEmailAddress)
                        sRedirectURL = sRedirectURL & "&password=" & Server.URLEncode(sPassword)

                        '// Uncomment this line if you want to redirect to the page.
                        'Call Response.Clear():  Call Response.Redirect(sRedirectURL)
                        
                        '// Debugging code to see what the URL is you'll be redirecting to.
                        Call Response.Write("<b>sRedirectURL</b> = [<u>" & Server.HTMLEncode(sRedirectURL) & "</u>]<br>" & vbNewLine)
                        Call Response.End()
                  End If
            End If
      End If %>
<html>
      <head><title>jay1971a's answer</title></head>
      <body>
            <form action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="post">
            <input type="hidden" name="IsPostback" value="True">
            
            <div style="width:600px;">
<%      If (sDisplayMessageHTML <> "") Then %>
            <div style="border:1px solid black;padding:10px;margin-bottom:20px;text-align:center;font-weight:bold;color:red;"><%=sDisplayMessageHTML%></div>
<%      End If %>

            <table>
                  <tr>
                        <td><b>Customer ID</b></td>
                        <td><input type="text" name="CustomerID" size="35" value="<%=Server.HTMLEncode(sCustomerID)%>"></td>
                  </tr>
                  <tr>
                        <td><b>Email Address</b></td>
                        <td><input type="text" name="EmailAddress" size="35" value="<%=Server.HTMLEncode(sEmailAddress)%>"></td>
                  </tr>
                  <tr>
                        <td><b>Password</b></td>
                        <td><input type="password" name="Password" size="35" value="<%=Server.HTMLEncode(sPassword)%>"></td>
                  </tr>
                  <tr><td colspan="2">&nbsp;</td></td>
                  <tr>
                        <td><input type="submit" value="Submit Info"></td>
                        <td><input type="button" value="Start Over" onclick="document.location.href = document.location.href;"></td>
                  </tr>
            </table>
            </div>

            </form>
      </body>
</html>
======================================================



-= DeathToSpam =-
0
 
LVL 9

Accepted Solution

by:
deathtospam earned 500 total points
Comment Utility
jay1971a --

Try this code out.  You'll only need to change two lines of code to get it to work:

1) Modify the value of the DATABASE_CONNECTION_STRING constant so that it points to your database.
2) Modify the value of sSqlQuery variable so that it checks your table to see if the Customer ID exists.

======================================================
<%     Option Explicit
     Response.Buffer = True

     Dim bIsPostback, sCustomerID, sEmailAddress, sPassword, sDisplayMessageHTML
     Dim sSqlQuery, oConnection, oRecordset, bCustomerIdExists, sRedirectURL
     
     '// NOTE: You need to enter this information to connect to your database.
     Const DATABASE_CONNECTION_STRING = "Driver={SQL Server};Server=SERVERNAME;Database=DATABASENAME;Uid=USERNAME;Pwd=PASSWORD;"

     bIsPostback = (Trim(Request.Form("IsPostback")) <> "")
     sDisplayMessageHTML = ""

     If (NOT bIsPostback) Then
          '// This is the user's first time visiting the page.
          sCustomerID = "":  sEmailAddress = "":  sPassword = ""
     Else
          '// User has been to the page before.
          sCustomerID = Trim(Request.Form("CustomerID"))
          sEmailAddress = Trim(Request.Form("EmailAddress"))
          sPassword = Trim(Request.Form("Password"))

          If (sCustomerID = "") Then
               '// ERROR: Customer ID is blank.
               sDisplayMessageHTML = "Please enter the value for the <b>Customer ID</b> field."
          ElseIf NOT IsNumeric(sCustomerID) Then
               '// ERROR: Customer ID was specified, but is not a valid number.
               sDisplayMessageHTML = "Please enter a valid number for the <b>Customer ID</b> field."
          ElseIf (sEmailAddress = "") Then
               '// ERROR: Email Address is blank.
               sDisplayMessageHTML = "Please enter the value for the <b>Email Address</b> field."
          ElseIf (sPassword = "") Then
               '// ERROR: Password is blank.
               sDisplayMessageHTML = "Please enter the value for the <b>Password</b> field."
          Else
               '// Check to see if the specified customer ID exists in the database.
               '// NOTE: You need to change "TableName" and "CustomerID" to reflect your table's info.
               'sSqlQuery = "SELECT 'True' AS [CustomerIdExists] FROM TableName WHERE CustomerID = " & sCustomerID
               sSqlQuery = "SELECT 'True' AS [CustomerIdExists] FROM client_batches WHERE client_id = " & sCustomerID

               Set oConnection = Server.CreateObject("ADODB.Connection")
               Call oConnection.Open(DATABASE_CONNECTION_STRING)
               Set oRecordset = oConnection.Execute(sSqlQuery)
                    bCustomerIdExists = (NOT oRecordset.EOF)
               Set oRecordset = Nothing
               Call oConnection.Close():  Set oConnection = Nothing

               If NOT bCustomerIdExists Then
                    '// ERROR: Customer ID is a valid number, but could not be found in the database.
                    sDisplayMessageHTML = "The customer ID you specified could not be found."
               Else
                    '// Here is where you create the URL you want to redirect to.
                    sRedirectURL = "http://www.somewhere.com/somepage.asp?customerID=" & Server.URLEncode(sCustomerID)
                    sRedirectURL = sRedirectURL & "&emailaddress=" & Server.URLEncode(sEmailAddress)
                    sRedirectURL = sRedirectURL & "&password=" & Server.URLEncode(sPassword)

                    '// Uncomment this line if you want to redirect to the page.
                    'Call Response.Clear():  Call Response.Redirect(sRedirectURL)
                   
                    '// Debugging code to see what the URL is you'll be redirecting to.
                    Call Response.Write("<b>sRedirectURL</b> = [<u>" & Server.HTMLEncode(sRedirectURL) & "</u>]<br>" & vbNewLine)
                    Call Response.End()
               End If
          End If
     End If %>
<html>
     <head><title>jay1971a's answer</title></head>
     <body>
          <form action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="post">
          <input type="hidden" name="IsPostback" value="True">
         
          <div style="width:600px;">
<%     If (sDisplayMessageHTML <> "") Then %>
          <div style="border:1px solid black;padding:10px;margin-bottom:20px;text-align:center;font-weight:bold;color:red;"><%=sDisplayMessageHTML%></div>
<%     End If %>

          <table>
               <tr>
                    <td><b>Customer ID</b></td>
                    <td><input type="text" name="CustomerID" size="35" value="<%=Server.HTMLEncode(sCustomerID)%>"></td>
               </tr>
               <tr>
                    <td><b>Email Address</b></td>
                    <td><input type="text" name="EmailAddress" size="35" value="<%=Server.HTMLEncode(sEmailAddress)%>"></td>
               </tr>
               <tr>
                    <td><b>Password</b></td>
                    <td><input type="password" name="Password" size="35" value="<%=Server.HTMLEncode(sPassword)%>"></td>
               </tr>
               <tr><td colspan="2">&nbsp;</td></td>
               <tr>
                    <td><input type="submit" value="Submit Info"></td>
                    <td><input type="button" value="Start Over" onclick="document.location.href = document.location.href;"></td>
               </tr>
          </table>
          </div>

          </form>
     </body>
</html>
======================================================



-= DeathToSpam =-
0
 

Author Comment

by:jay1971a
Comment Utility
That's excellent!

Precisely what I needed.

Thanks very much.

Jay.
0
 
LVL 9

Expert Comment

by:deathtospam
Comment Utility
No problem.  :)  It's definitely a lot more code than you should be expected to write on your own as a Classic ASP novice.  But hopefully it's straightforward enough that you can understand what's there, and how it all works.  Either way, I'm glad it works for you.


-= DeathToSpam =-
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now