Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Resulting ASP Form Problem

Status
Not open for further replies.

nidifice

Programmer
Oct 9, 2003
47
0
0
US
I have a form.asp page that 'POST's data into a result.asp. After the result.asp page loads, I want to send an exact copy of that page in an email. Suggestions?
 
Well that would most likely involve putting the page into a string then mailing it.

But I would suggest sending a link of the results, and save the results in a database you could easily make one design for your results, and access it via id number

results.asp?id=35235

If you need further help let me know.
- Jason
 
hi there

Do you want to be able to send a copy of the results by email when the user clicks something or automatically?

if it is automatically then what you need to do is nip to do is get your self a copy of formmail.asp and add your domain name in the top section of the code as this is the only line you need to change and then save this file anywhere you want on your site and you should renmae the formmail.asp, then all you need to do is add 5 hidden fields to your form to hold the email address the email is to sent too. one for the subject of the email, one for the redirect which is to be the results page and one for the order of the the way you want the fileds in the email to look. then you simply teel your orignal form to post to the formmail.asp and this page will send the email and then show the results page.

logical progression: form > formmail > results

let me know and i can give you the code for the formmails.asp page so that you simply hace to copy it and make a new file and i can give you a example form in code so that you can see the principle.

if you want the results to be sent if the user clicks a button on the results page it is a very similar proccess.

logical progression: form > results > formmail > confirmation of emial > results

for this doing it this way you simply put a form on the results page with all the hidden fields for the form and the ones formmail needs to send the email and then when they click the button and submit the form the form will send the page to the formmail and sen the email and the redirect caneither take them to a tconfirmation of results being sent page or simply straight back to the results page it is your choice i allway think a simple thank you page is nice.

here is the code for formmail.asp so copy this and save it as a page.

########## THERE ARE NO HTML OR BODY TAGS OK ###########

<%@ LANGUAGE=&quot;VBScript&quot; %>

<%

'Change the referers line only to reflect your domain name

referers = Array(&quot; &quot;yourdomainname.com&quot;)


'- End required customization section. -------------------------------------

fromAddr = Request(&quot;email&quot;)

Response.Buffer = true

errorMsgs = Array()

'Check for form data.

if Request.ServerVariables(&quot;Content_Length&quot;) = 0 then
call AddErrorMsg(&quot;No form data submitted.&quot;)
end if

'Check if referer is allowed.

validReferer = false
referer = GetHost(Request.ServerVariables(&quot;HTTP_REFERER&quot;))
for each host in referers
if host = referer then
validReferer = true
end if
next
if not validReferer then
if referer = &quot;&quot; then
call AddErrorMsg(&quot;No referer.&quot;)
else
call AddErrorMsg(&quot;Invalid referer: '&quot; & referer & &quot;'.&quot;)
end if
end if

'Check for the recipients field.

if Request.Form(&quot;_recipients&quot;) = &quot;&quot; then
call AddErrorMsg(&quot;Missing email recipient.&quot;)
end if

'Check all recipient email addresses.

recipients = Split(Request.Form(&quot;_recipients&quot;), &quot;,&quot;)
for each name in recipients
name = Trim(name)
if not IsValidEmail(name) then
call AddErrorMsg(&quot;Invalid email address in recipient list: &quot; & name & &quot;.&quot;)
end if
next
recipients = Join(recipients, &quot;,&quot;)

'Get replyTo email address from specified field if given and check it.

name = Trim(Request.Form(&quot;_replyToField&quot;))
if name <> &quot;&quot; then
replyTo = Request.Form(name)
else
replyTo = Request.Form(&quot;_replyTo&quot;)
end if
if replyTo <> &quot;&quot; then
if not IsValidEmail(replyTo) then
call AddErrorMsg(&quot;Invalid email address in reply-to field: &quot; & replyTo & &quot;.&quot;)
end if
end if

'Get subject text.

subject = Request.Form(&quot;_subject&quot;)

'If required fields are specified, check for them.

if Request.Form(&quot;_requiredFields&quot;) <> &quot;&quot; then
required = Split(Request.Form(&quot;_requiredFields&quot;), &quot;,&quot;)
for each name in required
name = Trim(name)
if Left(name, 1) <> &quot;_&quot; and Request.Form(name) = &quot;&quot; then
call AddErrorMsg(&quot;Missing value for &quot; & name)
end if
next
end if

'If a field order was given, use it. Otherwise use the order the fields were
'received in.

str = &quot;&quot;
if Request.Form(&quot;_fieldOrder&quot;) <> &quot;&quot; then
fieldOrder = Split(Request.Form(&quot;_fieldOrder&quot;), &quot;,&quot;)
for each name in fieldOrder
if str <> &quot;&quot; then
str = str & &quot;,&quot;
end if
str = str & Trim(name)
next
fieldOrder = Split(str, &quot;,&quot;)
else
fieldOrder = FormFieldList()
end if

'If there were no errors, build the email note and send it.

if UBound(errorMsgs) < 0 then

'Build table of form fields and values.

body = &quot;<table border=0 cellpadding=2 cellspacing=0>&quot; & vbCrLf
for each name in fieldOrder
body = body _
& &quot;<tr valign=top>&quot; _
& &quot;<td><b>&quot; _
& name _
& &quot;:&nbsp;</b></font></td>&quot; _
& &quot;<td>&quot; _
& Request.Form(name) _
& &quot;</td>&quot; _
& &quot;</tr>&quot; & vbCrLf
next
body = body & &quot;</table>&quot; & vbCrLf

'Add a table with any environmental variables.

if Request.Form(&quot;_envars&quot;) <> &quot;&quot; then
body = body _
& &quot;<p>&quot; _
& &quot;<table border=0 cellpadding=2 cellspacing=0>&quot; & vbCrLf
envars = Split(Request.Form(&quot;_envars&quot;), &quot;,&quot;)
for each name in envars
name = Trim(name)
body = body _
& &quot;<tr valign=top>&quot; _
& &quot;<td><b>&quot; _
& name _
& &quot;:&nbsp;</b></td>&quot; _
& &quot;<td>&quot; _
& Request.ServerVariables(name) _
& &quot;</td>&quot; _
& &quot;</tr>&quot; & vbCrLf
next
body = body & &quot;</table>&quot; & vbCrLf
end if

'Send it.

str = SendMail()
if str <> &quot;&quot; then
AddErrorMsg(str)
end if

'Redirect if a URL was given.

if Request.Form(&quot;_redirect&quot;) <> &quot;&quot; then
Response.Redirect(Request.Form(&quot;_redirect&quot;))
end if

end if %>

<html>
<head>
<title>Form Mail</title>
<style style=&quot;text/css&quot;>

body {
background-color: #ffffff;
color: #000000;
font-family: Arial, Helvetica, sans-serif;
font-size: 10pt;
}

table {
border: solid 1px #000000;
border-collapse: collapse;
}

td, th {
border: solid 1px #000000;
border-collapse: collapse;
font-family: Arial, Helvetica, sans-serif;
font-size: 10pt;
padding: 2px;
padding-left: 8px;
padding-right: 8px;
}

.error {
color: #c00000;
}

</style>
</head>
<body>

<% if UBound(errorMsgs) >= 0 then %>
<p class=&quot;error&quot;>Form could not be processed due to the following errors:</p>
<ul>
<% for each msg in errorMsgs %>
<li class=&quot;error&quot;><% = msg %>
<% next %>
</ul>
</td></tr></table>
<% else %>
<table cellpadding=0 cellspacing=0 width=450>
<tr style=&quot;background-color:#c0c0c0;&quot;>
<th colspan=2 valign=bottom>
Thank you, the following information has been sent:
</th>
</tr>
<% for each name in fieldOrder %>
<tr style=&quot;background-color:#ffffff;&quot; valign=top>
<td><b><% = name %></b>&nbsp;</td>
<td><% = Request.Form(name) %>&nbsp;</td>
</tr>
<% next %>
</table>
<% end if %>

</body>
</html>

<% '---------------------------------------------------------------------------
' Subroutines and functions.
'---------------------------------------------------------------------------

sub AddErrorMsg(msg)

dim n

'Add an error message to the list.

n = UBound(errorMsgs)
Redim Preserve errorMsgs(n + 1)
errorMsgs(n + 1) = msg

end sub

function GetHost(url)

dim i, s

GetHost = &quot;&quot;

'Strip down to host or IP address and port number, if any.

if Left(url, 7) = &quot; then
s = Mid(url, 8)
elseif Left(url, 8) = &quot; then
s = Mid(url, 9)
end if
i = InStr(s, &quot;/&quot;)
if i > 1 then
s = Mid(s, 1, i - 1)
end if

getHost = s

end function

function IsValidEmail(email)

dim names, name, i, c

'Check for valid syntax in an email address.

IsValidEmail = true
names = Split(email, &quot;@&quot;)
if UBound(names) <> 1 then
IsValidEmail = false
exit function
end if
for each name in names
if Len(name) <= 0 then
IsValidEmail = false
exit function
end if
for i = 1 to Len(name)
c = Lcase(Mid(name, i, 1))
if InStr(&quot;abcdefghijklmnopqrstuvwxyz_-.&quot;, c) <= 0 and not IsNumeric(c) then
IsValidEmail = false
exit function
end if
next
if Left(name, 1) = &quot;.&quot; or Right(name, 1) = &quot;.&quot; then
IsValidEmail = false
exit function
end if
next
if InStr(names(1), &quot;.&quot;) <= 0 then
IsValidEmail = false
exit function
end if
i = Len(names(1)) - InStrRev(names(1), &quot;.&quot;)
if i <> 2 and i <> 3 then
IsValidEmail = false
exit function
end if
if InStr(email, &quot;..&quot;) > 0 then
IsValidEmail = false
end if

end function

function FormFieldList()

dim str, i, name

'Build an array of form field names ordered as they were received.

str = &quot;&quot;
for i = 1 to Request.Form.Count
for each name in Request.Form
if Left(name, 1) <> &quot;_&quot; and Request.Form(name) is Request.Form(i) then
if str <> &quot;&quot; then
str = str & &quot;,&quot;
end if
str = str & name
exit for
end if
next
next
FormFieldList = Split(str, &quot;,&quot;)

end function

function SendMail()

dim mailObj
dim addrList

'Send email based on mail component. Uses global variables for parameters
'because there are so many.

SendMail = &quot;&quot;

'Send email
set mailObj = Server.CreateObject(&quot;CDONTS.NewMail&quot;)
mailObj.BodyFormat = 0
mailObj.MailFormat = 0
mailObj.From = fromAddr
mailObj.To = recipients
mailObj.Subject = subject
mailObj.Body = body
mailObj.Send
set mailobj=nothing
end function %>


######### END OF CODE ######################

here is an example of a basic form that you can customise to your needs.

########### EXAMPLE OF FORM TO SEND EMAIL ###############

<! Example Script for use with Formmail.ASP>

<html><center>

<! The forms action must match the name of the formail script>
<! If you rename the formmail script you must change this here >

<form action=&quot;formmail.asp&quot; method=&quot;post&quot;>

<! This is the From: address>
From:
<input type = text name=&quot;email&quot; value=&quot;websiteform@yourdomainname.com&quot;><br>


<! Example fields which will be sent in the email>

Name: <input type = text name=&quot;name&quot; ><br>
Your email: <input type = text name=&quot;email&quot; ><br>
Phone number: <input type = text name=&quot;phone_number&quot; ><br>
Comments: <input type = text name=&quot;comments&quot; ><br>

<!Hidden fields>

<input name=&quot;_subject&quot; type=&quot;hidden&quot; value=&quot;Site Feedback&quot;>
<input name=&quot;_recipients&quot; type=&quot;hidden&quot; value=&quot;webmaster@<yourdomain.com>&quot;>
<input name=&quot;_fieldorder&quot; type=&quot;hidden&quot; value=&quot;name,email,phone_number,comments,&quot; >
<input name=&quot;_redirect&quot; type=&quot;hidden&quot; value=&quot;thanks.htm&quot;>
<br><br>

<! Display the submit button>
<input type = submit value =&quot;Send Message&quot;>
</form>

</center></html>

########### END OF CODE #######################

this is a simply comments page. if you have a smtp server on your machine it will send emails if not you will have to upload it onto a web server to send the emails but it is som simple to use and their a because it collects the fields from your form it doesn't matter how many you have or how give it will send them in the order you put in the form. this means that you can have lots of different forms on your site using one formmail.asp page.

hope this helps you out.

cheers dean
 
I thought of both of these options (refering to Jason's reply) and I was trying to avoid the string (unless given a simple way to make a string out of a whole page) and the database won't work because its an intranet and the email is going external.

I am converting a ton of forms to an online intranet and most of them they simply want emailed here or there.

Something I thought of, but can't figure out, is to take the results.asp page after it loads from the server and save it, then attach it to my email. (Basically emulating the File > Save As, but automatically and saving to the server).

Is this possible?

Dustin
 
Hi Dustin

To get the page as a string why don't you use the XMLHTTP object?

Regards
Satish
 
I was playing with the XMLHTTP object, but its new to me so I wasn't for sure if it could do what I wanted it to.

In this results.asp page, the body contains :
<%=reqeust.form(&quot;variable&quot;)%>

So if I use the XMLHTTP object to create the string, what will those (<%=reqeust.form(&quot;variable&quot;)%>) sections look like?

I need them to be the actual result, not the code. Let me know if this reply isn't clear enough.
 
<%=reqeust.form(&quot;variable&quot;)%>
if it's copy pasted straight out of your page there's a typo in there.
has to be <%reqUEst.form(&quot;variable&quot;) %>
 
Not copy and pasted... just not proof reading.

 
Yup, I think that is due to the typo because you will never see code using XMLHTTP. If the form value is blank then there will be a blank there.

Regards
Satishkumar
 
There was no typo, but I'll try using the XMLHTTP object and reply with my results.
 
sorry, it seems your comment was just before mine. I did not see it before posting mine.

Anyway, you can post your form values to the page using XMLHTTP.

Regards
Satish
 
<%
Response.Buffer = True
Dim objXMLHTTP, xml
Set xml = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)

xml.Open &quot;POST&quot;, &quot; False
xml.Send &quot;varname=Dustin&varnumber=123456&quot;

Response.Write xml.responseText

Set xml = Nothing
%>

I've found code that works like this; but the &quot;POST&quot; information is written out after the 'xml.send' manually.
If the user fills out the form information, how can I have the 'xml.send' use that form for the 'POST' ?
 
To get the form values and send them to &quot;results.asp&quot; put all of this code in the page where the form is being submitted to then use
Code:
xml.send Request.Form

Regards
Satish
 
That would be perfect if it would work Satish.

I got this error with
Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)
--------------------------------------------
msxml3.dll error '80070057'

The parameter is incorrect.

/asset protection/frm_InvestigationReport_verify.asp, line 12
-----------------------------------------------

I got this one with
Server.CreateObject(&quot;MSXML2.ServerXMLHTTP&quot;)
-----------------------------------------------
msxml3.dll error '80004002'

No such interface supported

/asset protection/frm_InvestigationReport_verify.asp, line 12
---------------------------------------------------
 
Oh...and here is the exact code.

7 Response.Buffer = True
8 Dim objXMLHTTP, xml
9 Set xml = Server.CreateObject(&quot;MSXML2.ServerXMLHTTP&quot;)

11 xml.Open &quot;POST&quot;, &quot; False
12 xml.Send request.Form

14 Response.Write xml.responseText

16 Set xml = Nothing
 
remove the semi-colon after the URl in the open method.

Regards
Satishkumar
 
The forum put the semicolon there...it isn't in the code.

xml.Open &quot;POST&quot;, &quot;://localhost/asset%20protection/frm_investigatinreport_verify.asp&quot;, False

I left off the HTTP so it wouldnt' make this a link.
 
Sorry for the delayed response. Did you try a simple code like the &quot;imdb&quot; example to see if it works?

Regards
Satish
 
hi dustin -
we're trying to do exactly this -
did you manage to get it working? -

we can use the object to open another page - yahoo for example - immediately after our results.asp page and can use cdosys to scrape yahoo and send via email, but when we try to scrape off a page from our domain the browser just hangs, then gives a dns error....

we're thinking that maybe you can't use the object to suck out a page from the same server/domain let alone the exact same one....

Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top