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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    ACM
    ACM is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Duplicating the Output of a User's Input

    Okay, so I've created a JavaScript form which allows the user to enter their name into the form and display it within the sentence, "Welcome to the site ______". It works on one page, but, problem is, I'd like it to be displayed on every page. I'm aware you cannot do this with getElementbyId (can only be used once), which is what I've used. I've tried using getElementsbyClass, but from my understanding, it's not as simple as that.

    Here's my code so far anyway:

    Code:
    <script type="text/javascript">
    function inputname(){
    	var userInput = document.getElementById('userInput').value;
    	document.getElementById('welcome').innerHTML = userInput;
    }
    </script>
    Code:
    <p>Welcome to the site <b id='welcome'></b> </p> 
    <input type='text' id='userInput' />
    <input type='button' onclick='inputname()' value='Enter Name'/>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    To pass information of this kind from one page to another you should use a cookie. This has been covered many times in the forum.


    Code:
    <script type="text/javascript">
    
    function changeText2(){
    var userInput = document.getElementById('uInput').value;
    userInput = userInput.replace(/[^a-z\s\-\']/gi,"");    // letters space hyphen apostrophe only
    userInput = userInput.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    userInput = userInput + "!";
    document.getElementById('boldStuff2').innerHTML = userInput;
    document.getElementById("stuff").style.display="none";
    createCookie ("UserName",userInput,365);  // 365 days persistence
    }
    
    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else {var expires = ""}
    document.cookie = name+"="+value+expires+"; path=/";
    }
    
    // eraseCookie("UserName");  // use for testing - uncomment to erase the cookie
    
    function eraseCookie(name) {
    createCookie(name,"",-1);
    }
    
    // Everything below here excpt the lines in blue goes in your other pages as well as the home page.
    
    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }
    
    </script>
    </head>
    
    <body>
    
    <p>Welcome to the site <b id='boldStuff2'></b> </p> 
    <div id = "stuff" style="display:block">
    Please enter your name here <input type='text' id='uInput' onchange = "changeText2()">
    </div>
    
    <script type="text/javascript">
    // this script must come after the divs- best to place it immediately in front of the </body> tag
    if(readCookie('UserName')) {
    var n = readCookie('UserName');
    document.getElementById("boldStuff2").innerHTML =  n;
    document.getElementById("stuff").style.display="none";}
    
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 03-20-2012 at 09: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.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by ACM View Post
    Okay, so I've created a JavaScript form which allows the user to enter their name into the form and display it within the sentence, "Welcome to the site ______".
    A cookie is the last way I would use to transfer the person's name to another page unless you are restricted to doing this client-side only. The disadvantage of using cookies is that if the user has cookies disabled or clears the browser cache for some reason while on your website, the person's name will not appear on the page.

    If you want to make sure the person's name appears every time then you need to store the person's name in a server side session variable and then use it to display the name on every page as required. This will work every time whether cookies are enabled or not.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Quote Originally Posted by webdev1958 View Post
    If you want to make sure the person's name appears every time then you need to store the person's name in a server side session variable and then use it to display the name on every page as required. This will work every time whether cookies are enabled or not.
    ??? REALLY? PHP supports cookie-less sessions??

    Hmmm...yes, reading the docs, I see that it does! How does it do that? Pass the session id along as a query string when cookies are disabled?

    Ahhh...yes... found it: http://www.php.net/manual/en/session.idpassing.php

    I knew ASP.NET allowed this. It can pass the session id info 3 ways: In cookie, in querystring, or in a hidden <form. field. Since all normal pages in ASP.NET utilize a <form>, this works.
    Last edited by Old Pedant; 03-21-2012 at 03:59 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    Quote Originally Posted by Old Pedant View Post
    ??? REALLY? PHP supports cookie-less sessions??

    Hmmm...yes, reading the docs, I see that it does!
    I didn't even mention php

    I was making a general suggestion to make sure the op is at least aware there is a better server side solution and so my work in this thread is done

    Which, if any, they choose doesn't matter to me


    [/ot]

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Ah, but ASP only supports sessions via cookies. And JSP requires that you write a plugin to use anything but cookies. So your statement was really only true for ASP.NET and PHP (or JSP, *if* you are competent enough in Java).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    Quote Originally Posted by Old Pedant View Post
    Ah, but ASP only supports sessions via cookies. And JSP requires that you write a plugin to use anything but cookies. So your statement was really only true for ASP.NET and PHP (or JSP, *if* you are competent enough in Java).
    I don't see the point you are making, so all I can do is repeat my reply:

    I didn't even mention php

    I was making a general suggestion to make sure the op is at least aware there is a better server side solution and so my work in this thread is done

    Which, if any, they choose doesn't matter to me
    [/ot]

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by webdev1958 View Post
    A cookie is the last way I would use to transfer the person's name to another page unless you are restricted to doing this client-side only. The disadvantage of using cookies is that if the user has cookies disabled or clears the browser cache for some reason while on your website, the person's name will not appear on the page..
    Well, in that case cookies are useless for any purpose as of course the user can always disable or erase them, or may have disabled Javascript. Many sites such as onine banking require that cookies are enabled. Here the worst that that can happen is that the user's name is not shown.

    I would advise ACM to ignore this silly objection. "He only does it to annoy because he knows it teases" - Lewis Carroll

    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.

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    Well, in that case cookies are useless for any purpose....
    Not necessarily. I certainly wouldn't use cookies for critical features, but for non critical features I might use them.

    If my name didn't appear when I logged into my bank account I would wonder if somehow the account has been compromised or whether security has been breached somehow. My online bank doesn't use cookies to display my name when I am logged in.

    But like I said, I just want to make sure the op is aware there is a better server side solution but they can choose however they like to display the user's name

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by Old Pedant View Post

    There are two methods to propagate a session id:
    ■ Cookies
    ■ URL parameter

    The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs.
    Last edited by Philip M; 03-21-2012 at 09:36 AM.

    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.

  • #11
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs.
    That red bit is what I am suggesting, especially for critical features.
    Last edited by webdev1958; 03-21-2012 at 09:41 AM.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by webdev1958 View Post
    That red bit is what I am suggesting, especially for critical features.
    Displaying the visitor's name on ACM's website is hardly critical. And I have the idea that you might as well have made your suggestion in Klingon as far as ACM (1 post here) is concerned. I know you want to show everyone how amazingly clever you are, but you ought to take account of the OP's knowledge and ability range. You know perfectly well that many ISPs offering basic website hosting packages do not include a cgi-bin or access to server-side languages. So ACM is almost certianly restricted to doing this client-side only. Presumably he can work out whose suggestion is the most helpful to him.
    Last edited by Philip M; 03-21-2012 at 09:54 AM.

    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.

  • #13
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    Displaying the visitor's name on ACM's website is hardly critical.
    To you it obviously doesn't appear to be, but I doubt the op needs you to decide for him/her . I'm sure they can decide for themselves.

    Like I said, I just want to make sure the op is aware there is a better server side solution but they can choose however they like to display the user's name

    Quote Originally Posted by Philip M View Post
    You know perfectly well that many ISPs offering basic website hosting packages do not include a cgi-bin or access to server-side languages.
    No, I'm not sure that is correct at all especially after you claimed recently that IE9 does not support document.getElementsByClassName() when it clearly does.

    btw - Awardspace, for one, provides very good free web hosting
    Last edited by webdev1958; 03-21-2012 at 09:59 AM.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by webdev1958 View Post
    Like I said, I just want to make sure the op is aware there is a better server side solution but they can choose however they like to display the user's name
    Yes, sure, they can display it in Arabic or Chinese script if they wish. They do not need your graciously granted permission to choose to do what they want!

    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.

  • #15
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    They do not need your graciously granted permission to choose to do what they want!
    I agree 100% just as they do not need yours either


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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