Don E Posted October 9, 2011 Share Posted October 9, 2011 Not exactly. If you go here: http://w3schools.com/js/js_cookies.asp you'll see how to set cookies, check cookies, and get cookies. Link to comment Share on other sites More sharing options...
newtoajax Posted October 9, 2011 Author Share Posted October 9, 2011 This is not working, the getElementById(); may be the issue. // JavaScript Documentwindow.onload = getCookie; function getCookie(userData){var i,x,y, cookies = document.cookie.split(";"); for (i=0;i< cookies.length; i++){x = cookies[i].substr(0,cookies[i].indexOf("="));y = cookies[i].substr(cookies[i].indexOf("=") + 1);x = x.replace(/^\s+|\s+$/g,""); if ( x == userData){return unescape(y); }}} function setCookie(userData,value,expireDay){var expireDate = new Date();expiredate.setDate(expireDate.getDate() + expireDate);var userValue = escape(value) + ((expireDay == null) ? "" : ";expires="+expireDate.toDateString());document.cookie=userData + "=" + userValue;} function confirmUser() { var fName = document.getElementById("fname").value;var lName = document.getElementById("lName").value;var address = document.getElementById("address").value;var email = document.getElementById("email").value;var phone = document.getElementById("phone").value; var user = fName + lName + adress + email + phone; user = getCookie("user"); if (user != ""){window.location.replace("userRedirect.html"); } else {alert("Please submit a form and return to this page.\n Thank you.");}}[\code] Link to comment Share on other sites More sharing options...
newtoajax Posted October 9, 2011 Author Share Posted October 9, 2011 Had issue with chat, I do no know how to delete posts, sorry. Link to comment Share on other sites More sharing options...
newtoajax Posted October 9, 2011 Author Share Posted October 9, 2011 I am trying to get the cookie to stay in the form when I reload the page, and put all the id="" in a loop. I have to have this down by tomorrow, I am lost. Link to comment Share on other sites More sharing options...
Don E Posted October 10, 2011 Share Posted October 10, 2011 Hey newtoajax, I'm not quite sure what exactly you're trying to do, but since you're lost and need an idea of how it goes by tomorrow(probably too late by now), I thought I'd put together a webpage with a basic form with three input fields that submit to a function and sets three cookies for each input field and then shows(alerts) document.cookie so you can see the cookies. To fetch each name/value pair individually is a little bit more complicated and decided to leave that out for now. In other words, say I wanted to only alert() just the phone number below, it would require a lot more code to grab that from the document.cookie string. <!DOCTYPE html><html><head><title>JS Cookies/Form</title><script type="text/javascript">function insertData(form){var fname = form.fname.value;var lname = form.lname.value;var phone = form.phone.value; if(fname && lname && phone){ // set cookies var when = new Date(); when.setTime(when.getTime() + 24 * 60 * 60 * 1000); // 24 hours from now when.setFullYear(when.getFullYear() + 1); // One year from now document.cookie=escape("firstname")+"="+escape(fname)+";expires="+when.toUTCString(); document.cookie=escape("lastname")+"="+escape(lname)+";expires="+when.toUTCString(); document.cookie=escape("phone")+"="+escape(phone)+";expires="+when.toUTCString(); alert(document.cookie); // To see what's inside document.cookie return true;}else{ alert("Please fill in ALL form fields!"); return false;} }</script></head><body><h1>Form</h1><form method="post" action="#" onSubmit="return insertData(this);"><fieldset><legend>Insert Data</legend><label for="fname">First Name:</label><input type="text" name="fname" /><br/><label for="lname">Last Name:</label><input type="text" name="lname" /><br/><label for="phone">Phone:</label><input type="text" name="phone" /><br/><input type="submit" value="Submit Data" /></fieldset></form> </body></html> To see the alert above, use firefox or internet explorer because for some reason chrome shows up blank when doing alert(document.cookie);(Well for me at least); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.