View Full Version : Function not being called?

Apr 10th, 2014, 11:33 AM
Hi there everyone, I'm currently creating a website for a university assignment and am having to manipulate a user system due to the lack of support for databases on the server I am using, I suppose it's due to security issues at my university - so I'm having to manipulate it so it looks like it works to the user.

I am attempting to allow a user to register to the website, at which I am able to set a cookie using javascript containing their username, password and e-mail address, the login form will then check the browser to see if the cookies exist and whether they equal the same values that the user has inputed, thus letting them login. I realise the cookie will expire within an hour, but the idea of this website is to simulate a user system, not create one (first year of university).

I have this code that sets the cookies from the registration form, but for some reason it doesn't seem to work in the slightest - it just redirects the user back to the login form instead of alerting about the successful user registration.

Here is my code for my Javascript...

function submitRegistration(){
var username = document.getELementById("username").value
var password = document.getELementById("password").value
var now = new Date();
var time = now.getTime();
time += 3600 * 1000;
document.cookie =
'username=' + username +
'; expires=' + now.toUTCString() +
'; path=/';
document.cookie =
'password=' + password +
'; expires=' + now.toUTCString() +
'; path=/';
alert("Thank you " + username + ", you have now been registered!");

Now here is my form HTML...

<form onSubmit="submitRegistration();">
Username: <input type="text" id="username"/><br/>
Password:&nbsp; <input type="password" id="password"/><br/>
Confirm Password: <input type="password" id="confirmpassword" style="width: 49%"/><br/>
E-mail Address: <input type="text" id="email" style="width: 57%" /><br/>
<input type="submit" value="REGISTER" align="left" />
<input type="reset" value="RESET" align="right" /></form>

I have tried this with onSubmit and onClick, I have a feeling it could be a typo somewhere that I'm not seeing or something; or maybe I'm going the entirely wrong way about this. I am only a beginner.

Any help given would be greatly appreciated, and thank you for taking your time to read through the drivel I was forced to write to over-explain this complicated work-around.

Philip M
Apr 10th, 2014, 12:05 PM
var username = document.getELementById("username").value
var password = document.getELementById("password").value

javascript is case-sensitive. Correct that and it works.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Apr 11th, 2014, 03:42 PM
Thank you ever so much, this fixed my problem and I feel like it was a really foolish mistake due to something so simple causing issues.

I have now, for some reason reached the issue of being unable to check if a cookie exists, I know it has existence due to the fact that it is stored within the browser.
Here is my code so far...

function checkCookie()
var user = getCookie("username");
if (user == null)
document.getElementById("loginformcontent").innerHTML = 'DISPLAY LOGIN BOX HERE';
document.getElementById("loginformcontent").innerHTML = '<div id="loginformcontenttitle">WELCOME ' + user + '!</div><div id="loginformcontentbody">you are logged in</div></div>';

Now, I have this function being called once the body is loaded so therefore, the div should load immediately after based upon the code; and I have even tried calling the function once onclicking on an object - but there is still no output in the div.

Can anyone see any obvious reason why this shouldn't work? The cookie definitely exists and that's all I want to know, if it exists, show a welcome screen, if not then don't.

Once again, I'm extremely appreciative on any help given; Javascript and me just don't seem to agree on anything lately!

Philip M
Apr 11th, 2014, 06:05 PM
You do not have a function named getCookie() so naturally nothing happens.

Try this:-

<body onload = "dispCookie('username'); dispCookie('password')">

<script type = "text/javascript">

function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1) {return false}
endSlice = cookieStr.indexOf(";",startSlice+1);
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice);
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;

function dispCookie(isName) {
nValue = getCookie(isName); // returns the value of the cookie
alert (nValue); // for testing. alerts are regarded as obsolete and should be used only for testing purposes. Use DOM methods to display a message to the user.



If no cookie is found, the script returns false - not null.

I expect you realise that this is hopelessly insecure as anyone can see the contents of the cookie using javascript:alert(document.cookie);

And in your code the username and password can be a single space. You do not test that the password and confirm password match.