tags:

views:

263

answers:

1

Hi there,

I'm creating a simple form that takes a referal code and email address, stores the email address and then sends an email.

The form has some script that checks if the email address already exists in the table and also looks up another table to check to see if the referral code that they are entering exists in the other table.

In my page I have an error message displaying if the code doesn't exist and/or if the email address exists. However, if the form submits successfully, then I want the form to be replaced by a thank you message.

This is the code in my page;

<% 
sys_message = ""

If (Request.Form("form_submit") <> "") Then
    %><!--#include file="doreferral.asp"--><%
End If %>

[...]

<% If(sys_message <> "") Then
    Response.Write(sys_message)
End If %>
<form action="referral.asp#msg" name="frmRegister" id="form_referral" method="post">
    <input name="fldCode" type="text" class="quick-referral-input" id="fldCode" value="Enter agent code" onblur="if(this.value=='') 
    this.value='Enter agent code';" onfocus="if(this.value=='Enter agent code') this.value='';"/>
    <input name="fldEmail" type="text" class="quick-referral-input" id="fldEmail" value="Enter email address" onblur="if(this.value=='') 
    this.value='Enter email address';" onfocus="if(this.value=='Enter email address') this.value='';"/>
    <input type="hidden" name="form_submit" value="submitted" />
    <input type="image" class="quick-referral-submit" src="images/homepage/quick-referral-submit.gif" value="submit" alt="Submit button" 
    onClick="checkSignup();">
</form>

The form uses a doreferral.asp page to do the insert/checking;

<%
    fldCode      = replace(request.Form("fldCode"),"'","")
    fldEmail  = replace(request.Form("fldEmail"),"'","")

    '' //Check the submitted email against existing ones in the database
    set conn = server.CreateObject("ADODB.connection")
    conn.open(application("DATABASE"))
    qs = "SELECT COUNT(ReferredEmail) AS 'Count' FROM TenantReferral WHERE ReferredEmail = '" & fldEmail & "'"
    set rs = conn.Execute(qs)

    countEmail = rs("Count")

    set rs = nothing
    conn.close
    set conn = nothing

    If(countEmail >= 1) Then
     sys_message = sys_message & "<p class='referral-thanks'>This email address has already been referred.</p>"
    End If 

    ''//Check the submitted code against existing ones in the database
    set conn = server.CreateObject("ADODB.connection")
    conn.open(application("DATABASE"))
    qs = "SELECT COUNT(AgentReferralCode) AS 'CountCodes' FROM Customers WHERE AgentReferralCode = '" & fldCode & "'"
    set rs = conn.Execute(qs)

    CountCode = rs("CountCodes")

    set rs = nothing
    conn.close
    set conn = nothing

    If(CountCode < 1) Then
     sys_message = sys_message & "<p class='referral-thanks'>The referral code does not exist.</p>"
    End If 

    '' //Only Process the SQL if there is no sys_message
    If(sys_message = "") Then

     SQLfields = SQLfields & "ReferredCode, "
     SQLvalues = SQLvalues & "'"& Trim(fldCode) &"', "
     SQLfields = SQLfields & "ReferredEmail"
     SQLvalues = SQLvalues & "'"& Trim(fldEmail) &"'"

     SQL = SQL & "INSERT into TenantReferral ("& SQLfields &") VALUES ("& SQLvalues &")"
     response.Write(SQL)

     set conn = server.CreateObject("ADODB.connection")
     conn.open application("DATABASE")
     SET rs = conn.execute(SQL)

     ''//Email code goes here

     response.Redirect("referral.asp")

    End If
%>

I wondered if anyone could spot anything that I could change/add to display a thank you message, but not display the form. I have tried a few If statements around the form, but without success.

Thank you.

+1  A: 

Easy enough to do, you just need to move your logic around a little.

Logic could go a little like this:

  • Check for existance of any data to process - If your form has been sent anything then its a post back so you need to do something with the data
  • If everything is ok with your data processing then Response.Redirect to your thank you page (this has the benefit of stopping users from refreshing the page and possibly running your code twice)
  • If any errors then write them out (sys_message stuff)
  • Display the form

This way you don't need to worry about weather to display the form or not, if everything is ok it will just redirect off and the rest of the page won't process.

Ideally I would wrap the code in your include into a function/sub and have that included up the top, then you could call it anywhere in your code <%= ValidateForm() %> or similar.

And of course you should switch to ASP JScript :P

Pete Duncanson
In my experience, classic ASP gets along with VBScript much better than with JScript. :)
Martha
I used to think that too, but then I switched when I took a job on many years back and never looked back, such a nicer language to work with. I'm a JScript convert, maybe you should give JS another go, you can do wonders with it :)
Pete Duncanson