Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Function not being called?

    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...

    Code:
    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;
        now.setTime(time);
        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...

    Code:
    <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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Remotive (04-11-2014)

  • #3
    New Coder
    Join Date
    Jan 2008
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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...

    Code:
    function checkCookie()
    {
    var user = getCookie("username");
        if (user == null)
              {
                  document.getElementById("loginformcontent").innerHTML = 'DISPLAY LOGIN BOX HERE';
            }else{
                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!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You do not have a function named getCookie() so naturally nothing happens.

    Try this:-

    Code:
    <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.
    
    }
    
    </script>
    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.
    Last edited by Philip M; 04-11-2014 at 06:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •