ASP Form - Help Needed

aussie7

Perch
I have got the following form working and it emails the form contents to me

Code:
<% Dim objCDO
Set objCDO = Server.CreateObject("CDO.Message")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
' Outgoing SMTP server
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.myhost.com"
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'whether you use authentication on the server
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objCDOSYSCon.Fields.Update
' Update the CDOSYS Configuration
Set objCDO.Configuration = objCDOSYSCon
objCDO.To = "my email address"
'objCDO.Name = Request.Form.Item("firstName")
'objCDO.Firstname = Request.Form.Item("firstName")
'objCDO.Lastname = Request.Form.Item("lastName")
objCDO.From = Request.Form.Item("email")
objCDO.Subject = Request.Form.Item("Subject")
bodyHTML = Request.Form.Item("Comments")

objCDO.HTMLBody = bodyHTML
objCDO.Send
Set objCDO = Nothing
Set objCDOSYSCon = Nothing

%>

As you can see commented out in the code above I tried to get firstName and lastName from the form name field and it doesn't work ?(

I want to add more form fields and need to know what is the ASP code, to have the new fields emailed to me when the form is submitted

I want to add the following fields in my form:

First Name
Last Name
Phone Home
Phone Work
Fax
Address Line 1
Address Line 2
Suburb
State (chosen from dropdown menu)
Postcode

Any help appreciated and thanks for reading my post :]
 
First off, I would check to make certain that the form items you are trying to access are spelled correctly. They should match the "name=" attribute of the associated form field.

Next, I would check something ridiculous, like are the form fields really inside the form tags. For myself, when something that should be working isn't, I did something stupid.

Also, check on whether your form is using GET or POST. If it's GET, you should be looking at Request.QueryString("fieldname").

Finally, when I get fields from a form, I ask for Request.Form("fieldname") (or Request.QueryString("fieldname")) instead of going down to the Item level. I'm not certain that even exists.
 
Thank You for the reply ;)

Yes all my name attributes are inside the form tags and I'm using method="post"

I tried Request.Form instead of Request.Form.Item and it works fine

But when I try

Code:
objCDO.firstName = Request.Form("firstName")

I get the following error ?(

Code:
Microsoft VBScript runtime error '800a01b6' 

Object doesn't support this property or method: 'firstName' 

/contact.asp, line 17

What do I put instead of objCDO or instead of objCDO.firstName ?(
 
Like I said, it's usually something ridiculous that I missed. In this case, I completely missed that you are trying to set a property called objCDO.firstName, something that doesn't exist. If you are just looking to pass these fields in the email, then do something like:

objCDO.Body = "First Name: " & Request.Form("firstName") & vbCrLf
objCDO.Body += "Last Name: " & Request.Form("lastName") & vbCrLf

The first line is setting the body to the text shown. The second line is adding the last name. Each line has a carriage return at the end. You would continue until you have added all of the information you want to include in the message.

Like I said, its usually something simple that I miss.
 
If you want firstname and lastname to appear as the sender, then you should put them in the 'from' line. There is no method 'firstName' or lastName', afaik.

So,

firstName = Request.form("firstName")
lastName = Request.form("lastName")
email = Request.form("email")
strFrom = firstName & " " & lastName & "<" & email & ">"
objCDO.From = strFrom
etc

(think that should work, anyhow. Haven't tried it...)
 
The from method must be a qualified email address on the domain. The ReplyTo method should be used for a return email address:

objCDO.ReplyTo = Request.Form("email")

It's also a good idea to use regular expressions to validate all form input.

You can use "if/then" statements to show/hide text adjacent to the input fields with relevant error messages to make an "email smart form".

Take a look at the sample below. It sends an xhtml email with a link to the website and lists the time that the email was sent (for a dirrerent time zone than the server, use now()-.25 where .25 is representative of the day part time differential; just adjust the decimal value). Use drop-down menus for state and country and you'll not need to validate them unless the fields are required. If you'd like the page html for this, let me know an I'll post it:

Code:
<%
If Request.Form("send") = "Y" Then
'General form error processing:define variables
Dim errsts
errsts = "N"
Dim first_name
first_name="N"
Dim last_name
last_name = "N"
Dim email
email="N"
'Name check
If Request.Form("first_name")= "" Then
errsts= "Y"
first_name="Y"
End If
If Request.Form("last_name")= "" Then
errsts= "Y"
last_name="Y"
End If
Dim emlregEx
Dim emlregExm
emlregExm = True
'email check: regular expression definition
Dim eregEx
Set eregEx = New RegExp
With eregEx
.Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
.IgnoreCase = True
.Global = True
End With
emlregEx =Request.Form("email")
emlregExm = eregEx.Test(emlregEx)
If emlregExm = False then 
errsts="Y"
email="Y"
End If
'Send the message if there are no errors on the form
If errsts= "N" Then

Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iConf
Set iConf = CreateObject("CDO.Configuration")


Set iMsg.Configuration = iConf
iMsg.To = "[email protected]"
iMsg.From ="[email protected]"
iMsg.ReplyTo = Request.Form("email")
iMsg.Subject = "Information request from your Website"
iMsg.HTMLBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & vbCrLf _
		& "<html>" & vbCrLf _
		& "<head>" & vbCrLf _
		& " <title>Information request from your Website</title>" & vbCrLf _
		& " <meta http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">" & vbCrLf _
		& "</head>" & vbCrLf _
		& "<body bgcolor='#FFFFFF' text='#000000' >" & vbCrLf _
		& "  <p>The following request for information was sent from <a href=""http://www.yourdomain.com"">yourdomain.com</a> on " & now() & " Pacific Time.</p>" & vbCrLf _
		& "  <p><strong>Name:</strong> &nbsp;" & Request.Form("first_name") & "&nbsp;" & Request.Form("last_name") & "</p>" & vbCrLf _
		& "  <p><strong>Address:</strong> &nbsp;" & Request.Form("address")& "</p>" & vbCrLf _
		& "  <p><strong>City:</strong> &nbsp;" & Request.Form("city")& "</p>" & vbCrLf _
		& "  <p><strong>State:</strong> &nbsp;" & Request.Form("state") & "</p>" &  vbCrLf _
		& " <p><strong>Zip:</strong> &nbsp;" & Request.Form("zip")& "</p>" & vbCrLf _		
		& " <p><strong>Country:</strong> &nbsp;" & Request.Form("country") & "</p>" &  vbCrLf _
		& "  <p><strong>E-mail:</strong> &nbsp; <a href=mailto:" & Request.Form("email")& ">" & Request.Form("email")&  "</a></p>" & vbCrLf _
		& "  <p><strong>Phone:</strong> &nbsp;" & Request.Form("tel")& "</p>" & vbCrLf _
		& "<p><strong>Message:</strong> &nbsp;" & Request.Form("comments")& "</p>" & vbCrLf _
		& "</body>" & vbCrLf _
		& "</html>"
iMsg.Send 
Response.Redirect("thanksr.asp)
  End If
End If
%>
 
sonata said:
Like I said, it's usually something ridiculous that I missed. In this case, I completely missed that you are trying to set a property called objCDO.firstName, something that doesn't exist. If you are just looking to pass these fields in the email, then do something like:

objCDO.Body = "First Name: " & Request.Form("firstName") & vbCrLf
objCDO.Body += "Last Name: " & Request.Form("lastName") & vbCrLf

The first line is setting the body to the text shown. The second line is adding the last name. Each line has a carriage return at the end. You would continue until you have added all of the information you want to include in the message.

Like I said, its usually something simple that I miss.

Thanks for your ongoing help and assistance to help me get this working ;)

I tried your code above and get the following error message

Code:
Microsoft VBScript compilation error '800a03ea' 

Syntax error 

/contact.asp, line 28 

objCDO.Body += "Last Name: " & Request.Form("lastName") & vbCrLf
 
bro said:
If you want firstname and lastname to appear as the sender, then you should put them in the 'from' line. There is no method 'firstName' or lastName', afaik.

So,

firstName = Request.form("firstName")
lastName = Request.form("lastName")
email = Request.form("email")
strFrom = firstName & " " & lastName & "<" & email & ">"
objCDO.From = strFrom
etc

(think that should work, anyhow. Haven't tried it...)

Thanks for the help, this code works great and does exactly what I wanted to do originally before I decided to add extra fields ;)
 
aussie7 said:
Thanks for your ongoing help and assistance to help me get this working ;)

I tried your code above and get the following error message

Code:
Microsoft VBScript compilation error '800a03ea' 

Syntax error 

/contact.asp, line 28 

objCDO.Body += "Last Name: " & Request.Form("lastName") & vbCrLf

Should read

Code:
Microsoft VBScript compilation error '800a03ea' 

Syntax error 

/contact.asp, line 28 

objCDO.Body & "Last Name: " & Request.Form("lastName")
 
Thanks hafa, I tried the following code

Code:
objCDO.Body = "First Name: " & Request.Form("firstName") & vbCrLf
objCDO.Body & "Last Name: " & Request.Form("lastName")

and get an error ?(

Code:
Microsoft VBScript compilation error '800a03ea' 

Syntax error 

/contact.asp, line 18 

objCDO.Body & "Last Name: " & Request.Form("lastName")
------------^
 
aussie7 said:
Thanks hafa, I tried the following code

Code:
objCDO.Body = "First Name: " & Request.Form("firstName") & vbCrLf
objCDO.Body & "Last Name: " & Request.Form("lastName")

and get an error ?(

Code:
Microsoft VBScript compilation error '800a03ea' 

Syntax error 

/contact.asp, line 18 

objCDO.Body & "Last Name: " & Request.Form("lastName")
------------^


The Visual Basic Carriage Return Line Feed (vbCrLf) must be followed by a _; like this:

vbCrLf _

The purpose of this bit is to make reading the code easier. Please take a close look at how this is implemented along with the & symbol in the first sample code I posted for you.

I also noted that you've specified the objCDO.Body method twice; this should be defined only once and should always be follwed by the = sign, not the & sign for a definition. Again, please refer to the previously posted sample code.
 
To build your code, it's best to construct all your strings beforehand, then apply them to the object methods. This makes it more readable, and more easily edited later. So,

'initialise the string
strBody = ""

'add 'From Joe Soap' to the first line of the body
strBody = strBody & "From " & firstName & " " & lastName & vbCrlf

'add the text from the comments box on the form
strBody = strBody & Request.form("Comments") & vbCrLf

'add whatever else you want to the body string
strBody = strBody & "" 'or whatever

'then apply the completed string to the Body
objCDO.body = strBody



ASP expressions should physically always be on one line, btw. As hafa says, if you're going to break up an expression onto more than one line to help readability, you need to add _ to the end of each unfinished line. This is especially important to watch out for if you cut-and-paste code from the web. Sometimes you can copy a line feed along with it that shouldn't be there, because that's how it was formatted in a webpage. You need to check through and remove those, or add the _ character to the end of those lines.

(xhtml and regular expressions are probably a bit much all at once for a beginner...)
 
Thanks hafa and bro, yes this is a bit much for me to under stand as I'm a complete newbie at ASP

I'll read it again tomorrow and see if any of it makes sense then, Thanks again ;)
 
aussie7 said:
Thanks for your ongoing help and assistance to help me get this working ;)

I tried your code above and get the following error message...
Sorry about that, aussie. I've been doing too much VB.Net lately and slipped into that syntax. As inferred by bro, the correct syntax for the second line should be:

objCDO.Body = objCDO.Body & "Last Name: " & Request.Form("lastName")
 
sonata said:
Sorry about that, aussie. I've been doing too much VB.Net lately and slipped into that syntax. As inferred by bro, the correct syntax for the second line should be:

objCDO.Body = objCDO.Body & "Last Name: " & Request.Form("lastName")

I just tried the above code and got the following error ?(

Code:
Microsoft VBScript runtime error '800a01b6' 

Object doesn't support this property or method: 'Body' 

/contact.asp, line 17

I have no idea what's going on, anyone got any ideas ?(
 
aussie7 said:
I have no idea what's going on, anyone got any ideas ?(

Yes... Google 'Using CDOsys' 8o Shocking idea, I know, but there are plenty of complete code examples for it on the net.

Here's roughly what I normally use on a simple form. You'll need to set up an outgoing mailbox on your account, as it uses authentication. I didn't test this so no guarantees.

<%
'code not tested...

'assume form includes:
'lastName
'firstName
'email
'form_subject
'comments


dim strHost, strErr, mailSuccess
dim strLastName, strFirstName
dim strBody, strSendername, strSenderemail
dim strSubject, strTo, strCC, strBCC, strFrom, strReply


strHost = "mail.yourDomain.com" 'edit as necessary
strFormsName = "yourDomain mailform" 'a text name for the From line
strFormsEmail = "[email protected]" 'i.e. the outgoing mailbox
strEmPass = "thePassword" 'i.e. the password for the outgoing mailbox

strTo = "yourself<[email protected]>" ' edit as necessary

strSubject = "Website email: " & request.form("form_subject")

strFirstName = Request.form("firstName")
strLastName = Request.form("lastName")
strSendername = strFirstName & " " & strLastName
strSenderemail = Request.form("email")

strFrom = strFormsName & "<" & strFormsEmail & ">"
strReply = strSendername & "<" & strSenderemail & ">"
strCC = "" ' SEND COPY TO...
strBCC = "" 'SEND BLIND TO...

strBody = strBody & "From " & strSendername & vbCrlf
strBody = strBody & Request.form("Comments") & vbCrLf


strErr = ""


Const cdoBasic = 1 'Use basic (clear-text) authentication.
Const cdoSendUsingPort = 2
Dim iMsg
Dim iConf
Dim Flds
Dim sMsg

On Error Resume Next

set iMsg = CreateObject("CDO.Message")
set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strFormsEmail
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strEmPass
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strHost
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 100
.Update
End With



'Apply the settings to the message object
With iMsg
Set .Configuration = iConf
.To = strTo
.From = strFrom
.ReplyTo = strReply
If strCC <> "" Then
.CC = strCC
End If
.Subject = strSubject
.TextBody = strBody
End With

if iMsg.Send then
mailSuccess = false
On Error Resume Next
strErr = "There has been an error. The message was not sent."
else
mailSuccess = true
strErr = "Message sent."
End If

' cleanup mail objects
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

'****************************** what happened...

response.write "Sent?: " & mailSuccess & "<br>"
response.write strErr
%>
 
Hi Bro - thats an excellent bit of code - I think you should pop it in the ASP Code Snippets section for future reference.
 
Hi aussie7, always nice to see another sandgroper around! :)

W3Schools is an excellent source for the begginner. Start from the top, with their tutorial on ASP:
http://www.w3schools.com/asp/default.asp

You'll eventually get to this page, which is the bit about emails using CDOSYS:
http://www.w3schools.com/asp/asp_send_email.asp

They have live examples too, like this one about simple form coding:
http://www.w3schools.com/asp/showasp.asp?filename=demo_simpleform

It would help you enormously to first understand the basic rules of the language and how it's written (the syntax), how to assign variables, etc.

Good luck! :)
 
BluJag said:
Hi Bro - thats an excellent bit of code - I think you should pop it in the ASP Code Snippets section for future reference.

thanks, though I think it's based on Microsft's original example code, iirc... I've just written it that many times I actually remember it :)

When I get a chance to test some real code I'll put it in the snippets.

It would be nice to have a couple of code examples for all the installed components, actually, to save us scrambling about the net every time customer asks for something we've never used before. Commented code that's been tested on the jh servers, preferably; not just a bare code example pulled off a web page somewhere.

Volunteers sign up here ->

CDOsys - bro :) ---DONE
...
 
bro said:
...

Volunteers sign up here ->

CDOsys - bro :)
asp.net file upload - Hafa Yoiks! BluJag posted a variant of this already...should have checked first...I'll see what else I have up my sleeve...
 
Back
Top