PDA

View Full Version : Noob cookie help



Shawn R
04-11-2009, 05:01 PM
Hello. I'm trying to make this code work and it's giving me a headache. Either the cookie is not saving or it is not being read properly. Regardless of my input, it always says I have no itinerary to show (after submitting). Can anyone help?:(

Thank you,
Shawn


[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Hotel Reservation</title>

<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

function reserveForm() {
var myDate = new Date();
myDate.setDate(myDate.getDate() + 1);
var inDate = new Date();
var outDate = new Date();
document.cookie = encodeURI("first_name=" + document.forms[0].first_name.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("last_name=" + document.forms[0].last_name.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("address1=" + document.forms[0].address1.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("address2=" + document.forms[0].address2.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("city=" + document.forms[0].city.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("state=" + document.forms[0].state.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("zip=" + document.forms[0].zip.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("phone=" + document.forms[0].phone.value)
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("fax=" + document.forms[0].fax.value)
+ ";expires=" + myDate.toUTCString();

inDate.setYear(document.forms[0].checkin_year.value);
outDate.setYear(document.forms[0].checkout_year.value);
inDate.setMonth(document.forms[0].checkin_month.value);
outDate.setMonth(document.forms[0].checkout_month.value);
inDate.setDate(document.forms[0].checkin_day.value);
outDate.setDate(document.forms[0].checkout_day.value);
document.cookie = encodeURI("checkin=" + inDate.toUTCString())
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("checkout=" + outDate.toUTCString())
+ ";expires=" + myDate.toUTCString();
document.cookie = encodeURI("number_of_guests=" + document.forms[0].number_guests.value)
+ ";expires=" + myDate.toUTCString();
}

function showItinerary() {
if (document.cookie == 0)
alert("You do not have an existing itinerary!");
else {

var savedData = decodeURIComponent(document.cookie);
var dataArray = savedData.split("=");
var messageString = "Existing Itinerary:\n";
for (var i = 0; i < dataArray.length; ++i) {
messageString += dataArray(i) + "\n";
}
alert(messageString);
}
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>


<body>
<h1>Hotel Registration</h1>
<form action="" onsubmit="reserveform();" ><input type="button" name="show1" value="Show"
onclick="showItinerary();" /> an existing intinerary or create a
new one below.


<h2>Name and Address</h2>
<table>
<tr>
<td>First Name </td>
<td> <input name="first_name" value="First Name" /></td>
</tr>
<tr>
<td>Last Name </td>
<td><input name="last_name" value="Last Name" /></td>
</tr>
<tr>
<td>Street Address 1 </td>
<td><input name="address1" value="Address 1"size="50" /></td>
</tr>
<tr>
<td>Street Address 2 </td>
<td> <input name="address2" value="Address 2" size="50" /></td>
</tr>
<tr>
<td>City </td>
<td> <input name="city" value="City" size="30" />
</tr>
<tr>
<td>State </td>
<td><input name="state" value="State" size="5" /></td>
</tr>
<tr>
<td>Zip </td>
<td><input name="zip" value="Zip" size="10" /></td>
</tr>
<tr>
<td>Phone </td>
<td> <input name="phone" value="Phone" /> </td>
</tr>
<tr>
<td>Fax </td>
<td><input name="fax" value="Fax" /></td>
</tr>
</table>
<h2>Reservation Request</h2>
<p>Hotel Check In <select name="checkin_month">
<option selected="selected" value="Month">Month</option>
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select> <select name="checkin_day">
<option selected="selected" value="Day">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select> <select name="checkin_year">
<option selected="selected" value="Year">Year</option>

<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
</select><br />
Hotel Check Out <select name="checkout_month">
<option selected="selected" value="Month">Month</option>
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select> <select name="checkout_day">
<option selected="selected" value="Day">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select> <select name="checkout_year">
<option selected="selected" value="Year">Year</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
</select></p>
<p>No. of People <input name="number_guests" size=
"10" /></p>
<p><input type="submit" value="Submit Reservation" /></p>
</form>
</body>
</html>



[code]

Jleagle
04-14-2009, 12:35 PM
You can answer one of your questions by seeing if the cookie was actually created. If your using firefox you can go to Tools -> Privacy -> Show Cookies, then you can filter out the ones just from your website.