Click to See Complete Forum and Search --> : Can someone please help me with my form?


dante00
05-30-2004, 10:51 AM
This is the first time that i program in ASP.
I need to build a form page where the client is required to fill several field the others are facultatory.

I copied the code from a friend of mine and i have few problems:

1) In his site the required fields were: the name, company, country, phone password and the username and in mine they are all the same except the password and the user name instead i need to put the email.
2) Is it possible to send the information both to the database and my email?

Can someone please help me!!!
Am I going in the right way. This is the post.

NCit
06-01-2004, 08:20 AM
I'm writing a very small form application below. If you examine the below code, you will understand form processing.


<%
' Page name MUST BE the same as in the form action below

'declaring variables
Dim objConn, SQL
Dim strFirstName, strLastName, strEmail
Dim strMessage

'checking if form is submitted
'I always use a hidden form field (which is 'wf' here) to check the form submission
If Request.Form("wf") <> "" Then

strFirstName = Request.Form("frmFirstName")
strLastName = Request.Form("frmLastName")
strEmail = Request.Form("frmEmail")

'if every required fields are submitted then start form proccessing
'you may also check every field seperatelly to write a message about them
If strFirstName <> "" And strLastName <> "" And strEmail <> "" Then

'openning the connection
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("database_name.mdb")

'defining the SQL Query that will be executed
'replace the words in brackets with your database fields
SQL = "INSERT INTO [TABLE_NAME] " &_
"([FIRSTNAME], [LASTNAME], [EMAIL])" &_
"VALUES (" &_
"'" & strFirstName & "', " &_
"'" & strLastName & "', " &_
"'" & strEmail & "'" &_
")"

On Error Resume Next
objConn.Execute(SQL)
Response.Write SQL

If err.Description <> "" Then
strMessage = "An error occured, please try later."
Else
strMessage = "You have successfully submitted the below form."
End If

'if not every required field is filled
Else

strMessage = "Please fill every field in the form"

End If

End If
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script>
function CheckForValues(){
// function that will check the form fields
var strFirstName = document.frmRegister.frmFirstName;
var strLastName = document.frmRegister.frmLastName;
var strEmail = document.frmRegister.frmEmail;

if (strFirstName.value == '')
{alert('Please enter your First Name');strFirstName.focus();return false;}

if (strLastName.value == '')
{alert('Please enter your Last Name');strLastName.focus();return false;}

if (strEmail.value == '')
{alert('Please enter your Email');strEmail.focus();return false;}

document.frmRegister.submit();
}
</script>
</head>

<body>

<form action="form.asp" name="frmRegister" method="post" onSubmit="return false;">
<table>
<% If blnError Then %>
<tr>
<td colspan="2">ERROR_NOTE</td>
</tr>
<% End If %>
<tr>
<td>FirstName</td>
<td><input type="text" name="frmFirstName" value="<% =strFirstName %>"></td>
</tr>
<tr>
<td>LastName</td>
<td><input type="text" name="frmLastName" value="<% =strLastName %>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="frmEmail" value="<% =strEmail %>"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" onClick="CheckForValues();"></td>
</tr>
</table>
<input type="hidden" name="wf" value="1">
</form>

</body>
</html>


This is, of course, a very simple form proccessing application. You may develop a better form proccessing, like checking if the email is valid, or the First Name field is string only etc.

dante00
06-01-2004, 08:41 AM
First of all thank you for your answer.
I don't really know what to do , because the person that asked me the form doesn't want anymore connection to a database he just wants that the form should sent to his email, what code should i add?

THanks

CardboardHammer
06-01-2004, 08:52 AM
You need to look around for code that sends e-mail from ASP.

I don't code ASP any more, so I haven't got that kind of code myself (though I do have .NET code that does it).

I think people tend to use a COM object called CDO to do mailings from ASP. Here's one thread here that shows code using it: http://www.webdeveloper.com/forum/showthread.php?s=&threadid=33753

Perhaps someone else here can hook you up better.

NCit
06-01-2004, 12:45 PM
So if you want to send email instead of recording into database, then you have to delete SQL and Connection lines.


SQL = "INSERT INTO [TABLE_NAME] " &_
"([FIRSTNAME], [LASTNAME], [EMAIL])" &_
"VALUES (" &_
"'" & strFirstName & "', " &_
"'" & strLastName & "', " &_
"'" & strEmail & "'" &_
")"

On Error Resume Next
objConn.Execute(SQL)
Response.Write SQL


Change the above code with the code below;


Dim strHTML

strHTML = "FirstName : " & strFirstName
strHTML = strHTML & "<br>LastName : " & strLastName
strHTML = strHTML & "<br>Email : " & strEmail

Call SendMail("sender@yourdomain.com", "receiver@domain.com", "This is the subject", strHTML)

Sub SendMail(sFrom, sTo, sSubject, sBody)
Dim Mail, Conf
Set Mail=Server.CreateObject("CDO.Message")
Set Conf = CreateObject("CDO.Configuration")
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Conf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.yourdomain.com"
Conf.Fields.Update
Set Mail.Configuration = Conf
Mail.HtmlBody=strHTML
Mail.From="sender@yourdomain.com"
Mail.To="receiver@yourdomain.com"
Mail.Subject="This is the subject"
On Error Resume Next
Mail.Send
Set Conf=Nothing
Set Mail=Nothing
End Sub


But the code above will work only if CDO is enabled on your server. There are some third-party email components like JMail, ASPEmail, ASPMail etc. If you decide to use one of these components then SendMail subroutine must be changed according to that mail component syntax.

dante00
06-01-2004, 03:21 PM
Thank you again for your help
the server support told me that i can use only :
ASPMail, CDONTS, CDOSYS

I really feel stupid but this is my really first time programming asp (and i hink also the last), but what means : "SendMail subroutine must be changed according to that mail component syntax".
Please help!!!!

NCit
06-01-2004, 04:06 PM
I see that you have CDOSYS and CDONTS support. So you don't have to change anything in SendMail. Just use the code as is.

Also no one has learned ASP from birth, so don't call yourself like that. It's only a matter of time and experience.