<!-- JavaScript:Cookies code snippet 6. --> <!-- Passing cookie values from page to page using multiple cookies. --> <!-- First page --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- Code source: http://www.justfigures.co.uk/ --> <!-- A resource for web developers using XHTML, CSS, JavaScript, PHP --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>View code snippet page</title> <script language="JavaScript" type="text/javascript"> //<![CDATA[ // create a new Date object var expiryDate=new Date() // set the expiry date of the cookie to 100 days in the future: a persistent cookie expiryDate.setTime(expiryDate.getTime() + (1000*60*60*24*100)) /* function to add the values of the form elements to individual cookies, this function calls setCookie() */ // for function setCookie() see JavaScript: Cookies code snippet 3 function setValues(aForm){ setCookie("petNameCookie",aForm.txtPetName6a.value,expiryDate) setCookie("petSpeciesCookie",aForm.txtPetSpecies6a.value,expiryDate) } //]]> </script> </head> <body> <form name="frm6a" id="frm6a" onsubmit="return setValues(this)" action="6b_testCookieA.html"> <!-- where '6b_testCookieA.html' is the file name of the second page, change this as required --> <h1>First page</h1><br /> Please enter your pet's name:<br /> <input type="text" name="txtPetName6a" value="" size="60" /><br /> Please enter your pet's species type:<br /> <input type="text" name="txtPetSpecies6a" value="" size="60" /><br /><br /> <input type="submit" value="Proceed to page 2 of the form" /> </form> </body> </html> <!-- Second page --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- Code source: http://www.justfigures.co.uk/ --> <!-- A resource for web developers using XHTML, CSS, JavaScript, PHP --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>View code snippet page</title> <script language="JavaScript" type="text/javascript"> //<![CDATA[ /* function to retrieve the form elements from the individual cookies, this function calls getCookie() */ // for function getCookie() see JavaScript: Cookies code snippet 4 function getValues(){ var petNameValue6b=getCookie("petNameCookie") var petSpeciesValue6b=getCookie("petSpeciesCookie") if (petNameValue6b != false){ document.frmTestCookie6b.txtPetName6b.value = petNameValue6b } if (petSpeciesValue6b != false){ document.frmTestCookie6b.txtPetSpecies6b.value = petSpeciesValue6b } } //]]> </head> <!-- Note use of 'body' onload event --> <body onload="return getValues()"> <!-- note that the form's action has been cancelled --> <h1>Second page</h1><br /> <form name="frmTestCookie6b" action="#"> You have previously entered your pet's name:<br /> <input type="text" name="txtPetName6b" value="" size="60" /><br /> You have previously entered your pet's species type:<br /> <input type="text" name="txtPetSpecies6b" value="" size="60" /><br /> The form could now present more questions to the user.<br /> </form> </body> </html>