Question : Can I send a cookies' data to a server side database without reloading the page?

I am setting a cookie as follows.  Is there a way to use AJAX to send this cookie to a MySQL database immediately after the user clicks without refreshing the page?

<head>
function set_cookie(name,value) {
      document.cookie = name + "=" + value + ";"
}
</head>

<body>
     <a onclick="set_cookie(color, blue);">CLICK</a>
</body>

Answer : Can I send a cookies' data to a server side database without reloading the page?

Test page :

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
		function set_cookie(name,value) {
		      document.cookie = name + "=" + value + ";"
		      saveCookieToDatabase(name,value)
		}
function saveCookieToDatabase(cookieName, cookieValue) {
            var xmlhttp = null;
            if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
            if(xmlhttp != null) {
                xmlhttp.open("GET","saveCookieToDatabase.php?cookieName="+cookieName+"&cookieValue="+cookieValue,false);
                xmlhttp.send();
                alert(xmlhttp.responseText); // if the php code run successfully is good thing to echo a message
                return;
            }
            alert("bad news : ajax call failed !");
            return;
    }
</script>
</head>
<body>
	<a onclick="set_cookie(color, blue);">CLICK</a>
</body>
</html>
Random Solutions  
 
programming4us programming4us