Jump to content

[SOLVED] help getting value from a cookie


meomike2000

Recommended Posts

as i am still very new with working with ajax and js.... i need some help getting the value of a cookie......

 

this is all just for learning by the way....

 

the cookie is set when a php script validates a user,

if ($loginId == 'mike' && $password == 'mike11') {
//setcookie('userID', 12345);
setcookie("user", $loginId, mktime()+1209600);
$respType = 'success';
$respMsg = 'userhome.html';
}else{

 

i know this part works!

 

here is the ajax part....

look to  this.init = function()  @@@  self.user = document.cookie.value; ...

how should this be.....

 

var user = new function() {
this.ajax = null;
this.user = '';

this.init = function() {
	var self = user;
	self.user = document.cookie.value; 
	self.ajax = new Ajax();
	self.getinfo();
};

this.getinfo = function() {
	var self = user;
	self.ajax.doGet('/ajaxtest/getuser.php?user=' + self.user, self.setresult);	
};

this.setresult = function(str) {
	var self = user;
	var respArr = str.split(',');
	var name = respArr[0];
	var number = respArr[1];
	var userSpan = document.getElementById('userSpan');
	var userDiv = document.getElementById('userDiv');
	if (userSpan.firstChild) {
		userSpan.removeChild(userSpan.firstChild);
	}
	if (userDiv.firstChild) {
		userDiv.removeChild(userDiv.firstChild);
	}
	userSpan.appendChild(document.createTextNode(number));
	userDiv.appendChild(document.createTextNode(name));	
};

this.clearCookie = function(name) {
	var expireDate = new Date(0);
	document.cookie = name + '=; expires=' + expireDate.toGMTString() + '; path=/';
};

this.cleanup = function() {
	var self = user;
	self.ajax = null;
	self.user = null;
	self.clearCookie(user);
};

};

window.onload = user.init;
window.onunload = user.cleanup;

 

thanks mike.....

Link to comment
Share on other sites

ok i got it worked out...

 

this is the change that i made:

change in this.init      self.user = self.getCookie('user');

then added the getcookie function.......

and now all is working well.......

 

        this.init = function() {
	var self = user;
	self.user = self.getcookie('user'); 
	self.ajax = new Ajax();
	self.getinfo();
};

this.getcookie = function(c_name) {
	var self = user;
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.