Join 300,477 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,759 people online right now. Registration is fast and FREE... Join Now!
Well here I am again, I have my form code all set, thanks to PsychoCoder. I now have to add a cookie to the form so that once the form has been completed and submitted it will tell the user that they already submitted the form. I borrowed PsychoCoder's "cookie snippet" as a place to start, but I am not sure how to get the cookie to validate and return a notification page that I have written. I will post the form coed here:
CODE
<html> <head>
<!-- Timothy Shannon WEB406 - Week 4 Individual Assignment Complete Change Request - #2 on Service Request - SR-kf-011 --> <!-- Page title --> <title>Kudler Fine Foods Specials and Events Mailing List Registration Form</title>
<script type="text/javascript">
<!-- Define cookie -->
set_cookie("LoggedIn",true,10);
function set_cookie(fname, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); var expires = "; expires=" + date.toGMTString(); } else var expires = "";
var host = "your_domain.com"; var domain = "; domain=" + host; document.cookie = fname + "=" + value + expires + domain + "; path=/"; }
get_cookie("LoggedIn");
function get_cookie(fname) { var EQ = fname + "="; var ca = document.cookie.split(';'); for (var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(EQ) == 0) return c.substring(EQ.length,c.length); } return null; }
<!-- Form validation -->
function submitForm() { if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") { window.alert("You must enter your first and last names."); return false; } else { if (document.forms[0].address.value == "" && document.forms[0].email.value == "") { window.alert("You must enter either a mailing address or email address."); return false; } else { return true; } } } </script> </head>
<tr style="background-color: red"> <td colspan="2" align="center" valign="bottom"> <address style="font-family: times-new roman; font-size: 0.6em; font-style: normal; font-size: small; color: black"> Kudler Fine Foods · Shopping the World for the Finest Foods · La Jolla · Del Mar · Encinitas · </address> </td> </tr> </table>
</form>
</html>
And I will post the notification page code here:
CODE
<html> <head> <title>Form Already Processed</title> </head> <body> <script type="text/javascript"> document.write("<h1>Your information is already on file!</h1><h2>You have already registered for our list!</h2>"); var formData = location.search; formData = formData.substring(1, formData.length); while (formData.indexOf("+") != -1) { formData = formData.replace("+", " "); } formData = unescape(formData); var formArray = formData.split("&"); for (var i=0; i < formArray.length; ++i) { document.writeln(formArray[i] + "<br />"); } </script> </body> </html>
Thank you all again in advance for any help offered, I have received nothing but help here in the past and I've got to tell you it's a GREAT feeling having somewhere you can go for help and actually get answers.
I just don't seem to grasp the cookie thing and I don't understand why. I've read the text multiple times read the tutorial here and it just doesn't sink in. Please if someone could just give me some direction, like what am I missing, do I have to read the cookie at some point and do I have to tie it in to the onsubmit call? I could barely sleep last night thinking about this, and I woke up thinking about it, help!!!
Sorry for the delay, I work alot. Take a look at these modifications, I added comments to what I did to try and help you understand what Im doing
jscript
function submitForm() { //check and see if the cookie has been set yet if(!get_cookie("FormSubmitted") == "yes") { if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") { window.alert("You must enter your first and last names."); return false; } else { if (document.forms[0].address.value == "" && document.forms[0].email.value == "") { window.alert("You must enter either a mailing address or email address."); return false; } else { //now lets set the cookie. I'm creating a cookie //named FormSubmitted with a value of "yes" that //lasts 30 days set_cookie("FormSubmitted", "yes", 30) return true; } } } else { //in this example Im just alerting the user //you can redirect or do it how you need to alert("You've already submitted this once before"); } }
Thank you for your response and your suggested changes, I wasn't sure if I should just make the changes you indicated to the code I posted above or if I should start with my original code (without your snippets added). So I added the suggestions you made to the original code (without your snippets) and now the form submits without validating the fields and I can keep submitting the form so I'm not sure if the cookie is being set. I have been trying to figure out what is causing the form to just submit without doing the validation but to no avail, I'm still stuck! I followed your comments and I understand what you have done to add the cookie but somewhere something is causing the code not to do the validation. If you have a chance to take another look I would really appreciate it, I will copy the code as I have it now here:
CODE
<html> <head>
<!-- Timothy Shannon WEB406 - Week 4 Individual Assignment Complete Change Request - #2 on Service Request - SR-kf-011 --> <!-- Page title --> <title>Kudler Fine Foods Specials and Events Mailing List Registration Form</title>
<script type="text/javascript">
<!-- Form validation -->
function submitForm() { if(!get_cookie("FormSubmitted") == "yes") { if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") { window.alert("You must enter your first and last names."); return false; } else { if (document.forms[0].address.value == "" && document.forms[0].email.value == "") { window.alert("You must enter either a mailing address or email address."); return false; } else { set_cookie("FormSubmitted", "yes", 30) return true; } } } else { alert("You've already submitted this once before"); } }
<tr style="background-color: red"> <td colspan="2" align="center" valign="bottom"> <address style="font-family: times-new roman; font-size: 0.6em; font-style: normal; font-size: small; color: black"> Kudler Fine Foods · Shopping the World for the Finest Foods · La Jolla · Del Mar · Encinitas · </address> </td> </tr> </table>
</form>
</html>
Once again thank you in advance for your help, I need to turn in the assignment tonight, so I am hoping you will come to my rescue. :-)
Aaaaarrrrrggggghhhhhhhh! Something isn't right, I can't even get the form to validate even when I cut and paste the original form validation code in place of the set cookie/form validation code that was suggested, and I'm fried and bleary eyed! Thank you PsychoCoder for all your help you have been great, if you do get a chance to take another peek at this I would love to find out why I couldn't get it to work, that will be a real learning experience, talk about learning from mistakes. Is 49 too old to be switching careers and trying to become a programmer?
Here is the complete code for setcookie, get cookie and Ckeckcookie.
CODE
<html> <head> <script type="text/javascript"> function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return ""; }
function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); }
function checkCookie() { username=getCookie('username'); if (username!=null && username!="") { alert('Welcome again '+username+'!'); } else { username=prompt('Please enter your name:',""); if (username!=null && username!="") { setCookie('username',username,365); } } } </script> </head>