Screenshot
Introduction
This is the second version of the other feedback form I submitted. I am resubmitting it because the bugs in the older version and the updating procedure takes a hell lot of time, and also it should be noted that I have become modest now (see article's title!).
This is my second contribution to CodeProject so don't be angry with me if you think that this code is below you! I have been working on this during my school vacations (took me 32 minutes for building and debugging this on Jan 12, 2004). This is a simple feedback form which allows your website's users to send you feedback directly to your email ID. And to prevent spamming of your inbox, it has been configured so that each user can give feedback to you only once in a week (cookies)! It even mails their PC info (IP, Browser Info), the time it was posted, and the number of times you have received feedback! The form disables right click on the page without any prompt, and also you can bookmark CodeProject and set it as your homepage! All this is in the source files!
Background
If you have basic knowledge about cookies and CDONTS, then this tutorial is as easy as a pie! Even if you don't know anything about cookies and CDONTS, you can just download the companion ZIP containing the source files, change the email address to your own in a text editor like Notepad, and upload it into your web server which supports ASP and CDONTS, to watch the magic!
The code
Here's the code for checking whether the user has already given you feedback in the past week:
strfeed = Request.Cookies("Feed")("Yes")
IF (Len(strfeed) > 0) then
Response.Write "<font color=red><b>You have already" _
" given us feedback! You can give it again only " _
"after a week!</b></font>"
Response.Redirect "Index.asp"
End IF
Here's the code for requesting the form content:
strnam = Trim(Request.Form("name"))
stremail = Trim(request.form("mail"))
strcom = Trim(request.form("com"))
strcom = Trim(request.form("sug"))
In the above code, the form elements are:
- name - The name of the user (textbox)
- mail - The Email ID of the user (textbox)
- com - Comments from the user (textarea)
- sug - Suggestions from the user (textarea)
Here's the code for checking if the form fields are filled or not, and submitting the form if yes:
if (strnam <> "" and stremail <> "" and strcom <> "" _
and strsug <> "")then
dim newmail, strfeed, ipadd, brow, now, msgbody, date, _
strnam, stremail, strcom, strsug
num = application(num)
ipadd = Request.ServerVariables("REMOTE_ADDR")
brow = Request.ServerVariables("HTTP_USER_AGENT")
date = time
msgbody = "Suggestions: " & strsug & "Comments: " & strcom & _
vbcrlf & "PC info:" & "IP:" & ipadd & "Browser: " & brow & _
"Time:" & date & "Hits:" & num &"times!")
Set Newmail = server.CreateObject ("cdonts.newmail")
Newmail.From = stremail
Newmail.To = "someone@somewhere.com"
Newmail.Subject = "Feedback From:" & strnam
Newmail.Body = msgbody
Newmail.Send
Set Newmail = Nothing
Analysis: This code will retrieve the form fields and mail them with the IP address, browser info and time signed, to the email address 'someone@somewhere.com'! Before this code works, you need to add the following code declarations so as to enable counting the number of hits:
Dim Num
Num = Request.Servervariables("SCRIPT_NAME")
Application.Lock
IF isEmpty(Application(Num)) Then
Application(Num)=0
END IF
Application(Num) = Application(Num)+1
Application.Unlock
Here's the code for setting a cookie that expires after a week:
Response.Cookies("Feed")("Yes") = "yes"
Response.Cookies("Feed").Expires = DateAdd("d",7,Now())
Response.Redirect "index.asp"
Analysis: Here we are setting the value yes to the cookie 'Feed
' which when checked by the first bit of code will redirect the page to 'index.asp' if the value is greater than 0, i.e. (Len(strfeed)>0
). After setting the cookies, the page will automatically be redirected towards 'index.asp'.
As a bonus, I am including the code for disabling right-click on an ASP page without even a prompt! Add this code into your body
section! Here's the code:
<script language="JavaScript">
<!--
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e){
if (document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;
}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")
</script>
Go here for an ever lasting sample!
Note: Change the email (someone@somewhere.com) address to the address where you prefer to receive the feedback, in a text editor like Notepad, and upload it into your web server which supports ASP and CDONTS, to watch the magic!
Please rate it if you do like it! Comments and suggestions are also welcome!