2 Replies - 1155 Views - Last Post: 14 June 2011 - 02:20 PM

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Cookie decode is garbled numbers and letters

Posted 14 June 2011 - 11:18 AM

I am attempting to put the username in a cookie, so I can use it to populate a field on a different page. The only problem is the cookie prints out as a huge string of numbers and letters, instead of the username. All hints welcome. Here is the code:

login page code (just snippets, the body and other tags are there):

<script language="javascript" type="text/javascript">
/* <[CDATA[ */
        //Put the login name in a cookie for later use
        document.cookie = "name=" + encodeURIComponent(document.forms[0].username.value);    	 
/* ]]> */

	<form action="login.php" method="post" enctype="application/x-www-form-urlencoded">
		<dl>
			<dt><label>Login Name: </label></dt>
			<dd><input type="text" name="username" class="input"/></dd>
			<dt><label>Password: </label></dt>
			<dd><input type="password" name="password" class="input" /></dd>
			<input type="submit" name="loginBtn" value="Enter Site" class="button" />
		</dl>
	</form>



and the decode:

	function populateInspector()
	{
		if (document.cookie)
		{
			var queryData = decodeURIComponent(document.cookie);
			
			var queryArray = queryData.split("; ");	
			
			document.forms[0].inspector.value = queryArray[0].substring(queryArray[0].lastIndexOf("=") + 1);
		}
	}

		<dt><label>Date: </label></dt>
		<dd><input type="text" name="date" id="date" /></dd>
		<dt><label>Inspector: </label></dt>
		<dd><input type="text" name="inspector" /></dd>	



Is This A Good Question/Topic? 0
  • +

Replies To: Cookie decode is garbled numbers and letters

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Cookie decode is garbled numbers and letters

Posted 14 June 2011 - 12:27 PM

You could save yourself all that work and do it via PHP if it doesn't have to be in Javascript.

setcookie('username', $username); //set it


<input type="text" name="inspector" value="<?php echo $_COOKIE['username'] ?>" /> <!-- read it to your input element -->

Was This Post Helpful? 1
  • +
  • -

#3 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Cookie decode is garbled numbers and letters

Posted 14 June 2011 - 02:20 PM

Thank you codeprada, once again you have hit the nail on the head.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1