ASP "submit" to DB & Mail (CDOSYS) in one operation

PHTech

Senior Member
Joined
Aug 21, 2006
Messages
588
Reaction score
0
Location
Witbank
Hi there... As I am now totally fed up with ASP (and still forced to work with it), how the hell do I Submit Data to a database AND send an email using the form variables... I have tried different ways, even by using 2 pages, but can only manage to do one or the other function (mail OR database entries).

Below is the code of the page with the form, and the second post is the code for the second page sending the mail. PLEASE...! Can someone PLEASE help me with this stupid ASP crap...!!!

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/connection1.asp" -->
<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
If (CStr(Request("MM_insert")) = "formail") Then
  If (Not MM_abortEdit) Then
    ' execute the insert
    Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_connection1_STRING
    MM_editCmd.CommandText = "INSERT INTO feedback (Email, FirstName, FirstName) VALUES (?, ?, ?)" 
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 50, Request.Form("email")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 202, 1, 50, Request.Form("subject")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 202, 1, 50, Request.Form("message")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close
  End If
End If
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback</title>
<link href="assets/css/SiteGlobal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
	font-family: Tahoma, Geneva, sans-serif;
}
-->
</style></head>

<body leftmargin="0" topmargin="0">
<div class="HomeHeader"></div>
<div class="DivWrapper">
  <div class="MainTable">
    <p class="Date">comments &amp; feedback</p>
    <div class="SideNav">
      <p class="SideNavHeads"><a href="index.html" class="SideNavLinks">&raquo; home</a> <a href="#" class="SideNavLinks">&raquo; viewable threads</a><a href="witbank/index.html" class="SideNavLinks"></a></p>
      <p class="SideNavHeads">Related Items</p>
      <p class="SideNavHeads"><a href="links.html" class="SideNavLinks">&raquo; links</a> <a href="#" class="SideNavLinks">&raquo; latest updates</a><a href="http://chhzrhsrv650/EN/Pages/default.aspx" class="SideNavLinks">&raquo; xstrata plc</a> <a href="search.html" class="SideNavLinks">&raquo; search</a></p>
      <p class="SideNavHeads">Administration</p>
      <p class="SideNavHeads"><a href="pages/admin/adm_login.asp" class="SideNavLinks">&raquo; it administration</a></p>
      <p class="SideNavHeads"><a href="#"></a></p>
    </div>
    <p class="Heading1">Feedback</p>
    <div class="MainInfo">
      <p>Please give us your feedback on this intranet site. You can add suggestions, comments, something you want us to add or anything related to this intranet site. </p>
    </div>
    <div class="NormalText1">
      
<form name="formail" method="POST" action="feedback_sendmail.asp">
  <table align="center" cellpadding="3" cellspacing="1">
<tr> 
<td>Email</td> 
<td><input type="text" name="email" /></td> 
</tr> 
<tr> 
<td>Subject</td> 
<td><input type="text" name="subject" /></td> 
</tr> 
<tr> 
<td>Message</td> 
<td><textarea name="message"></textarea></td> 
</tr> 
<tr> 
<td colspan="2"> <input type="submit" value="send" name="submit" /> </td> 
</tr>
</table>
  <input type="hidden" name="MM_insert" value="formail" />
</form> 

    </div>

    <p>&nbsp;</p>
  </div>
  <p class="Footer">Copyright &copy; 2009 Xstrata Coal South Africa. All rights reserved | <a href="#" class="FooterLinks">Internet Usage Policy</a> | <a href="#" class="FooterLinks">IT Department</a> | <a href="#" class="FooterLinks">Credits</a></p>
</div>
</body>
</html>
 
...code of the second page.

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/connection1.asp" -->
<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
If (CStr(Request("MM_insert")) = "form1") Then
  If (Not MM_abortEdit) Then
    ' execute the insert
    Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_connection1_STRING
    MM_editCmd.CommandText = "INSERT INTO feedback (FirstName) VALUES (?)" 
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 50, Request.Form("hiddenField")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close
  End If
End If

' onLoad="document.form1.submit()"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback</title>
</head>

<body>

<p>

<% 

' Check whether you should send the email
'if Request.QueryString("send") = "1" AND Request.Form("submit") = "send" then 

Dim objMail, objConfig 

' Create a new email instance
Set objMail = Server.CreateObject("CDO.Message") 

' Create a new configuration object
Set objConfig = Server.CreateObject ("CDO.Configuration")

' Set configurations
With objConfig 

' SMTP Server (be sure to change this configuration!)
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.vodamail.co.za" 

' SMTP Port
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

' CDO Port
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

' Timeout 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
.Fields.Update 

End With 

' Apply configurations
Set objMail.Configuration = objConfig 

' Customize the email
objMail.From = "[email protected]" ' Sender
objMail.To = Request.Form("email") ' Receiver
objMail.Subject = Request.Form("subject") ' Subject
objMail.HTMLBody = Request.Form("message") ' Message

' Send email
objMail.Send() 

Set objMail = Nothing 

Response.Write("The email has been sent to the following recipient " &_ 
Request.Form("email"))

'Dim SubmitForm

SubmitForm = "document.form1.submit()"

'END IF

%>
  
</p>

<form ACTION="<%=MM_editAction%>" METHOD="POST" id="form1" name="form1">
  <input name="hiddenField" type="hidden" id="hiddenField" value="<%request.form("message")%>" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
 
Why over 2 pages? Take what you had before, and make sure you don't redirect until both operations have completed. Can easily be done on one page.
 
Its pretty easy doing that. And using Dreamweaver doesn't help dude...

put in a hidden field somewhere in your form like I told you in the previous thread (<input type="hidden" name="posted" value="true">)

Then at the top of your page:
Code:
If Request("posted") = "true" Then
<-- Insert the Feedback form into your database -->
<-- Send the email -->
End If

Based off your code:
<-- Insert the Feedback form into your database -->
Code:
Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_connection1_STRING
    MM_editCmd.CommandText = "INSERT INTO feedback (Email, FirstName, FirstName) VALUES (?, ?, ?)" 
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 50, Request("email")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 202, 1, 50, Request("subject")) ' adVarWChar
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param3", 202, 1, 50, Request("message")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close


Based off your code:
<-- Send the email -->
Code:
Dim objMail, objConfig 

' Create a new email instance
Set objMail = Server.CreateObject("CDO.Message") 

' Create a new configuration object
Set objConfig = Server.CreateObject ("CDO.Configuration")

' Set configurations
With objConfig 

' SMTP Server (be sure to change this configuration!)
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.vodamail.co.za" 

' SMTP Port
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

' CDO Port
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

' Timeout 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
.Fields.Update 

End With 

' Apply configurations
Set objMail.Configuration = objConfig 

' Customize the email
objMail.From = "[email protected]" ' Sender
objMail.To = Request("email") ' Receiver
objMail.Subject = Request("subject") ' Subject
objMail.HTMLBody = Request("message") ' Message

' Send email
objMail.Send() 

Set objMail = Nothing

So your feed back form construction would look like this:

*Include Files (connections etc)
*If Statement Above
*HTML Header Start
*Body
*Form
*End Body
*End HTML

Your form will then submit to the same page. So if you have it called feedback.asp. You will submit the form to feedback.asp.

The input hidden field I suggested you have in there is only true *AFTER* the execution of the server side code, so the if statement will fail and load the page normally.

Once you submit the page THEN it's true to itself and will fire the If statement. This will then insert it into the database and after the insert it will send the email.

You may choose to do a Response.Redirect "thankyou.asp" so that you can thank the person for filling in the form, or have another If statement within the form itself that response.write's a message saying thanks and displays the entire form based of if it submitted or not.

1 Page to do 2 (or more) things at the same time.

If the database insert fails the email won't send though. So you might want to do an On Error Resume Next at the top of the page but then you won't see the failures (if any) on the code execution.
 
OK, but according to my previous used scripts, why won't it let me do both at the same time...
 
FYI, as a tip (some might disagree) I don't use Request.Form or Request.Querystring specifically. I always use Request on itself (as per my examples based on your code)

The reason for that is it makes testing simple and if I do need to (for some reason) do something via the Querystring I can without having to modify the code.

Using Command's like you do (yay for Macromedia?) you don't have to worry much about SQL Injection attacks but you might want to try and clean up the data anyway. If you're interested I'll give you a function.
 
Your previous script (other thread) was checking for post, then running DB script and redirecting. Then it checked again for post, and did the email script. Problem is it was never getting that far.

The scripts you have on this page are not sending form post data between the two pages. That would explain why it's not happening either.
 
Top
Sign up to the MyBroadband newsletter
X