flipandboef
Posts: 107
|
Posted: 04/09/2008, 9:37 AM |
|
Hi all,
For my forms to be submitted I'm using the code below. This works great, however...
I now want to create a form where the e-mail is not required from a visitor, but is optional. The code however does require an email address..
Anyone an idea how I can set a default value in this code when no e-mail address is entered?
Example: User enters no email address, "noreply@test.com" should be used. User enters "Hello@itsme.com" in the e-mail address, then "Hello@itsme.com" must be used..
Anyone any ideas about this?
P.s.: I'm not an ASP guru, so keep it a little simple if you can
<%
option explicit
dim pde : set pde = createobject("scripting.dictionary")
pde.add "%mailing%", "bobby@myserver.com"
pde.add "%webmaster%", "jimmy@myserver.com"
pde.add "%info%", "billy@myserver.com"
function getTextFromFile(path)
dim fso, f, txt
set fso = createobject("Scripting.FileSystemObject")
if not fso.fileexists(path) then
getTextFromFile = ""
exit function
end if
set f = fso.opentextfile(path,1)
if f.atendofstream then txt = "" else txt = f.readall
f.close
set f = nothing
set fso = nothing
getTextFromFile = txt
end function
dim redir, mailto, email, subject, item, body, cc, bcc, message, html, template, usetemplate, testmode
redir = request.form("redirect")
mailto = request.form("mailto")
if pde.exists(mailto) then mailto = pde(mailto)
cc = request.form("cc")
bcc = request.form("bcc")
email = request.form("email")
if email = "" then email = pde("%email%")
subject = request.form("subject")
message = request.form("message")
template = request.form("template")
testmode = lcase(request.form("testmode"))="no"
if len(template) > 0 then template = getTextFromFile(server.mappath(template))
if len(template) > 0 then usetemplate = true else usetemplate = false
dim msg : set msg = server.createobject("CDO.Message")
msg.subject = subject
msg.to = mailto
msg.from = email
if len(cc) > 0 then msg.cc = cc
if len(bcc) > 0 then msg.bcc = bcc
if not usetemplate then
body = body & message & vbcrlf & vbcrlf
else
body = template
end if
for each item in request.form
select case item
case "redirect", "mailto", "cc", "bcc", "subject", "message", "template", "html", "testmode"
case else
if not usetemplate then
if item <> "email" then body = body & item & ": " & request.form(item) & vbcrlf & vbcrlf
else
body = replace(body, "[$" & item & "$]", replace(request.form(item),vbcrlf,"<br>"))
end if
end select
next
if usetemplate then 'remove any leftover placeholders
dim rx : set rx = new regexp
rx.pattern = "\[\$.*\$\]"
rx.global = true
body = rx.replace(body, "")
end if
if usetemplate and lcase(request.form("html")) = "yes" then
msg.htmlbody = body
else
msg.textbody = body
end if
if testmode then
if lcase(request.form("html")) = "yes" then
response.write "<pre>" & vbcrlf
response.write "Mail to: " & mailto & vbcrlf
response.write "Mail from: " & email & vbcrlf
if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
response.write "Subject: " & subject & vbcrlf & string(80,"-") & "</pre>"
response.write body
else
response.write "<html><head><title>Sendmail.asp Test Mode</title></head><body><pre>" & vbcrlf
response.write "Mail to: " & mailto & vbcrlf
response.write "Mail from: " & email & vbcrlf
if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
response.write "Subject: " & subject & vbcrlf & vbcrlf
response.write string(80,"-") & vbcrlf & vbcrlf & "<span style=""color:blue;"">"
response.write body & "</span>" & vbcrlf & vbcrlf
response.write string(80,"-") & vbcrlf & "**END OF EMAIL**</pre></body></html>"
end if
else
msg.send
response.redirect redir
end if
set msg = nothing
%>
An example form in html that uses this script:
<html>
<head>
<title>example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<Script language="JavaScript" type="text/JavaScript"><!--
function PR_TMForm(){
var theForm = document.TMForm;
var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
var sFlg_TermsAgree = false;
var rFlg_NDisplay = false;
var errMsg = "";
var setfocus = "";
for(var s6=0;s6<theForm['TermsAgree'].length;s6++){if(theForm['TermsAgree'].options[s6].selected){if(theForm['TermsAgree'].options[s6].text==theForm['TermsAgree'].options[0].text)sFlg_TermsAgree=true;}}for(var r3=0;r3<theForm['NDisplay'].length;r3++){if(theForm['NDisplay'][r3].checked)rFlg_NDisplay=true;}
if (theForm['z2_Security'].value == "" || (theForm['z2_Security'].value != theForm['z1_Security'].value)){
errMsg = "the Verification Word is not correct";
setfocus = "['z2_Security']";
}
if (sFlg_TermsAgree){
errMsg = "you MUST agree with the Terms and Conditions";
setfocus = "['TermsAgree']";
}
if (theForm['email'].value != ""){
if (!emailRE.test(theForm['email'].value)){
errMsg = "Please enter a valid e\-mail address";
setfocus = "['email']";
}
}
if (!rFlg_NDisplay){
errMsg = "Please select how your name will be displayed";
setfocus = "['NDisplay'][0]";
}
if (theForm['Name'].value == ""){
errMsg = "Please enter your Name \(abbreviations allowed\)\r\rExample\: John Smith or J.S.";
setfocus = "['Name']";
}
if (theForm['Message'].value == ""){
errMsg = "Please share your experience in the Message field";
setfocus = "['Message']";
}
if (errMsg != ""){
alert(errMsg);
eval("theForm" + setfocus + ".focus()");
}
else theForm.submit();
}</script>
</head>
<body>
<form action="http://www.mydomain.com/sendmail_cdo.asp" method="post" name="TMForm" id="TMForm">
<input type="hidden" name="redirect" value="http://www.mydomain.com/TYTestim.htm">
<input name="mailto" type="hidden" id="mailto" value="%webmaster%">
<input name="subject" type="hidden" id="subject" value="Website Form Submission">
<table cellspacing="2" cellpadding="0" border="0">
<tr valign="top"><td><p align="left">Message:<sup>*</sup> </p></td><td>
<textarea id="textarea" name="Message" rows="5" wrap="VIRTUAL" cols="35"></textarea></td></tr><tr valign="top"><td><p align="left">Name:<sup>*</sup> </p></td><td><input id="Name" maxlength="45" size="46" type="text" name="Name"></td></tr><tr valign="top"><td><p align="left">Display:<sup>*</sup> </p></td><td><label><input name="NDisplay" type="radio" value="Name" checked> My entered name</label> <label><input type="radio" name="NDisplay" value="Anonymous"> My name as Anonymous</label> <br><br></td></tr><tr valign="top"><td><p align="left">E-mail:<sup>*</sup> </p></td><td nowrap><input name="email" type="text" id="email" size="46" maxlength="45"> <br></td></tr><tr valign="middle"><td>Select:<sup>*</sup> </td><td nowrap><p><label><select name="TermsAgree" size="1" class="Terms" id="TermsAgree"> <option value="Disagree" selected>I Disagree</option> <option value="Agree">I Agree</option> </select> with the terms & conditions</label></p></td></tr></table>
<table cellspacing="0" cellpadding="0" width="98%" border="0">
<tr valign="top">
<td width="563" valign="middle"> <br>
If everything is filled in, type in the verification word in the box below
and hit "Send"<br>
</td>
</tr>
</table>
<table width="65%" border="0" cellspacing="0" cellpadding="0"><tr><td>Note: <sup>*</sup>are required fields<br><br><br><input name="Send" type="button" id="Send" onClick="PR_TMForm();" value="Send"> <input name="Reset" type="reset" id="Reset" value="Clear all fields"></td></tr></table><input name="template" type="hidden" id="template" value="/TestimTpl.htm"> <input name="html" type="hidden" id="html" value="yes"></form>
</body>
</html>
|