Unusual Error

windows

Guest
I'm working on making a sample hotel reservation form to show a friend how its done using this code:<br />
<br />
<script type="text/javascript"><br />
<!-- HIDE FROM INCOMPATIBLE BROWSERS<br />
function setCookie() {<br />
<br />
var expiresDate = new Date();<br />
expiresDate.setDate(expiresDate.getDate() + 1);<br />
document.cookie = encodeURI("user_name="<br />
+ document.forms[0].user_name.value) + "; expires="<br />
+ expiresDate.toUTCString();<br />
document.cookie = encodeURI("user_roomnumber="<br />
+ document.forms[0].user_roomnumber.value)<br />
+ "; expires=" + expiresDate.toUTCString();<br />
document.cookie = encodeURI("user_checkindate="<br />
+ document.forms[0].user_checkindate.value)<br />
+ "; expires=" + expiresDate.toUTCString();<br />
document.cookie = encodeURI("user_checkoutdate="<br />
+ document.forms[0].user_checkoutdate.value)<br />
+ "; expires=" + expiresDate.toUTCString();<br />
<br />
<br />
}<br />
<br />
<br />
function getUserPrefs() {<br />
var userPrefs = decodeURI(document.cookie);<br />
var prefs[]= userPrefs.split("; ");<br />
var userName = prefs[0].split("=");<br />
var userRoomnumber = prefs[1].split("=");<br />
var userCheckindate = prefs[2].split("=");<br />
var userCheckoutdate = prefs[3].split("=");<br />
window.alert("Welcome " + userName[1]<br />
+ ". Your room number is " + userRoomnumber[1]<br />
+ ". Your check in time is " + userCheckindate[1]<br />
+ ". Your check out time is " + userCheckoutdate[1]);<br />
}<br />
// STOP HIDING FROM INCOMPATIBLE BROWSERS --><br />
</script><br />
</head><br />
<body ><br />
<h1> Hadley Hotel Reservation form </h1><br />
<form><br />
<p>Name: <input type="text" name="user_name" /><br /><br />
Room number: <input type="text" name="user_roomnumber" /><br /><br />
Check in date: <input type="text" name="user_checkindate" /><br /><br />
Check out date:<input type="text" name="user_checkoutdate" /></p><br />
<p><input type="button" value="Submit" onclick="setCookie();" /></p><br />
<p><input type="button" value="Set Cookie" onclick="setCookie();" /></p><br />
<p><input type="button" value="Iteniary" onclick="getuserPrefs();" /></p><br />
</form><br />
</body><br />
</html><br />
<br />
But when i tried to run it i got an error message saying that the following line:<br />
var prefs[]= userPrefs.split("; ");<br />
was "expecting ";"<br />
What am i doing wrong? (i apologize if this is in the wrong forum)<!--content-->Not sure, but do you need to unescape the cookie, i.e.<br />
<br />
<br />
<br />
var userPrefs = decodeURI(unescape(document.cookie));<br />
<br />
<br />
<br />
I just wonder if your cookie has no ";" in it as it is escaped.<!--content-->No, i do not need to unescape the cookie. I simply want to make the cookies and i dont know why the line is question expects ';' when there is already both neccesary semicolons there in the line.<!--content-->var prefs= userPrefs.split("; ");<!--content-->Ok,,fang's advice DID help,,but now im plauged with another error as i cant get any of the buttons on the page to work,,any advice?<!--content-->Camel-case on onclick="getuserPrefs(); is getUserPrefs()<!--content-->I cant stop the errors. I tried running it and when i hit "iteniary" i got a message saying that the [1] in this line was null or invalid:<br />
var userRoomnumber = prefs[1].split("=");<br />
I tried getting rid of the numbers but that just caused it to say that the object could not support the function. What exactly am i doing wrong here?<!--content-->What does document.cookie return?<!--content-->document.cookie DOES show the text boxes where info is entered, but none of the submission buttons at the bottom of the screen work, and when iteniary<sp> is clicked, i get an error message from IE saying that [1] is null or not a valid object.<!--content-->Change all cookies like this:<br />
document.cookie = "user_name="+ escape(document.forms[0].user_name.value) + "; expires="+ expiresDate.toUTCString();<br />
and in getUserPrefs()<br />
var userPrefs = unescape(document.cookie);<br />
<br />
Have a look at this cookie tutorial (<!-- m --><a class="postlink" href="http://hotwired.lycos.com/webmonkey/98/29/index1a.html">http://hotwired.lycos.com/webmonkey/98/29/index1a.html</a><!-- m -->)<!--content-->
 
Back
Top