ArsanGamal
ArsanGamal

Reputation: 170

display cookie immediately

iam creating a php website in which i want to create a changable background color .. so i created a $_GET['color'] variable .. if the user click red the url will be http://localhost/?color=red and it sets a cookie with the chosen color my proplem that if the user clicks for example yellow the browser needs to refresh one time to get the new $_COOKIE['color'] to change the background color.. i want the to get the $_COOKIE variable to change the background color without any refreshing immediately when the user clicks the yellow link ??

Upvotes: 0

Views: 67

Answers (3)

Shijin TR
Shijin TR

Reputation: 7768

Use javascript for getting cookie value

  function getCookie(name)
   {
   var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
   }
   var color=getCookie("color");
  </script>

Upvotes: 0

Rednas
Rednas

Reputation: 609

you want to change the background without refreshing the page? i suggest to use jQuery ( or javascript ) for that.

just be sure you trigger the on click event when you click on the link to change the background. if you use jQuery you can try something like:

$('a.yellow').click(function(){
  $('body').css('background-color','yellow');
});

Upvotes: 0

ybo
ybo

Reputation: 17152

You can use css to dynamically change the background color of your page. Actually, you don't even need to contact the server for that, cookies are a client side feature that can be set using javascript.

EDIT: Here's a fiddle demonstrating that: http://jsfiddle.net/JvrVA/

Upvotes: 1

Related Questions