I have managed to gather some help and got some script working to create a cookie, and then read it back on another page and display the results.
Basically I create a cookie of the last hotel they looked at, then on their return the name of that hotel is displayed on the index page.
What i would also like to do, is grab the url, store that in the cookie to, then when displaying the name, the url is wrapped around it, so on clicking it takes the person back to that hotel.
All the code is below, the first part works fine, but i dont seem to be able to grab the url, and read it back without errors. As you will see, I have tried to use PHP, but its doesnt work out.
And here is when I read it back
Can anybody help me grab that url, store it then read it back through the id messageBlock in that div.
Cheers
Basically I create a cookie of the last hotel they looked at, then on their return the name of that hotel is displayed on the index page.
What i would also like to do, is grab the url, store that in the cookie to, then when displaying the name, the url is wrapped around it, so on clicking it takes the person back to that hotel.
All the code is below, the first part works fine, but i dont seem to be able to grab the url, and read it back without errors. As you will see, I have tried to use PHP, but its doesnt work out.
Code:
<body onload="createCookie('hotelcookie', '<?=$rows['Nom_Hot']?>', 365, '<? $url = $_SERVER['SERVER_NAME']; $page = $_SERVER['php_SELF']; echo "[URL unfurl="true"]http://".$url.$page;[/URL] ?>')">
<script type = "text/javascript">
function createCookie(name,value,days,url) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+url+"; path=/";
}
</script>
And here is when I read it back
Code:
<body onload="readCookie('hotelcookie')">
<div id="messageBlock"></div>
<script type = "text/javascript">
function readCookie(name,url) {
var nameEQ = name + "=";
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(nameEQ) == 0) {
hotel = c.substring(nameEQ.length,c.length);
//alert (hotel);
document.getElementById("messageBlock").innerHTML = "On your last visit you viewed "+"<a href=\"" + url + "\">" + hotel +"</a>";
return hotel;
}
}
return null; // no display if no previous visit
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
Can anybody help me grab that url, store it then read it back through the id messageBlock in that div.
Cheers