9 Replies - 990 Views - Last Post: 31 May 2012 - 12:45 PM

#1 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

Am i doing something wrong in my code as it just don't want to wor

Posted 29 May 2012 - 05:29 AM

Hi peeps

can you please check my code and point out where i am going wrong with this code

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex"  />
<title>Login</title>
<link rel="stylesheet" href="css/login.css" /> 
<script type="text/javascript">
function forgetMe() {
 	document.cookie = 'username = ; expires=Mon, 01 Jan 2000 00:00:00 GMT; path=/  ';
}

function logIn() {
	if (document.getElementById('remember').checked = true) then {
		var user = document.getElementById('user').value;
		var date = new Date();
		date.setTime(date.getTime()+15);
		var expires = "; expires="+date.toGMTString();
		document.cookie = Username+"="+user+expires+"; path=/";
	}
}

function notice() {
	ans = alert("This will save your username to this machine for 15 days. /nTo agree press ok or Cancel to stop this option");
	if (ans = false) then {
		document.getElementById('remember').checked = false;
	}
}
</script>
</head>
<body onload="document.getElementById('user').focus()">
	<div id="loginForm">
		<h1 align="center">Log In</h1>	
		<form>
			<div id="user">
				<label for="user">Username</label><br />
				<input name="user" type="text" id="user" value="<?php if(isset($_COOKIE['username'])){echo $_COOKIE['username'];} ?>" />
			</div>
			<div id="pass">
				<label for="pass">Password</label><br />
				<input name="pass" type="password" id="pass" maxlength="15" />
			</div>
			<br />
			<input name="remember" type="checkbox" id="remember" onclick="notice()"/><label for="remember">Remember Me</label><br />
			<a onclick="forgetMe()" id="forget">Forget me</a>
			<br />
			<br />
			<input name="submit" id="submit" type="button" value="Log in" onclick="javascript: logIn()" />
		</form> 
	</div>
</body>
</html>



according to google inspect element i have these errors:

Uncaught SyntaxError: Unexpected token { login.php:13
Uncaught ReferenceError: logIn is not defined login.php:47

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Am i doing something wrong in my code as it just don't want to wor

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 29 May 2012 - 05:32 AM

There is no then keyword in JS.

Simply,
if(){

}else{

}

This post has been edited by chinchang: 29 May 2012 - 05:33 AM

Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,235
  • Joined: 08-June 10

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 29 May 2012 - 05:33 AM

itís if() else, not if() then.

be aware that = is assignment, not comparison (thatís == or ===).

tip:
if ("value" == some_variable)

throws an error in case you accidentally use = instead of == or ===.
Was This Post Helpful? 1
  • +
  • -

#4 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 29 May 2012 - 05:51 AM

thanks took that out and it works when i click the checkbox now, just gotta get the cookie creation working now,

thanks for help
Was This Post Helpful? 0
  • +
  • -

#5 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 29 May 2012 - 09:12 AM

with regard to this line in my code

var user = document.getElementById('user').value;



i added
alert(user);


and i get the response undefined.

i checked that the id is there and spelt correctly

what else can i check?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,235
  • Joined: 08-June 10

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 29 May 2012 - 09:57 AM

anything in the Error Console?
Was This Post Helpful? 0
  • +
  • -

#7 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 31 May 2012 - 01:04 AM

sorry for late reply and no nothing in error console

here is my code that i am using which gives me the undefined variable

if (document.getElementById('remember').checked == true) {
		var user = document.getElementById('user').value;
		
		alert(user);

		/*var date = new Date();
		date.setTime(date.getTime()+15);
		var expires = "; expires="+date.toGMTString();
		document.cookie = Username+"="+user+expires+"; path=/";*/
	}
}


i know the id is correct as i used .id and that gave me the elements id correctly

This post has been edited by Kersh86: 31 May 2012 - 01:04 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,235
  • Joined: 08-June 10

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 31 May 2012 - 02:56 AM

do you have a live page?
Was This Post Helpful? 0
  • +
  • -

#9 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 31 May 2012 - 12:29 PM

Simply try this and check in console:
console.log(document.getElementById('user'));


What does it print in console?
Was This Post Helpful? 0
  • +
  • -

#10 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

Re: Am i doing something wrong in my code as it just don't want to wor

Posted 31 May 2012 - 12:45 PM

hi sry forgot to mention i got it solved earlier today,

i changed id's from user to username and it just worked after that, cant say why

and no live page sry using wamp to test

thanks anyway
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1