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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    trying to store user entered info please help

    Hi,

    I am trying to write a small javascript program where I can call the following two functions and store information which a user enters. The problem is if I call the function a second time to add more information the array gets overwritten.

    info ()
    insert (name, surname, favouriteColour)

    function info ()
    {
    name = prompt ("Please enter your name.");
    surname = prompt ("Please enter your surname.");
    favouriteColour = prompt ("Please enter your favourite colour.");
    }

    function insert (name, surname, favouriteColour)
    {
    var info = new Array ();
    info.name = name;
    info.surname = surname;
    info.favouriteColour = favouriteColour;
    return info
    };
    Help is really appreciated..
    Last edited by kev3kev3; 12-12-2011 at 07:10 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Have a look at this recent thread which is in effect the same question:-

    Help with basic arrays and funcitons

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.


    It is your responsibility to die() if necessary….. - PHP Manual

    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
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi, I've tried to change the previous post, i hope it has worked.

    I did read that thread that you posted, the only problem being that with that you have to specify how many cars you want to enter.

    I want to be able to keep repeating the functions from a main menu and adding as many as i want during the session. The data doesn't need to be stored between sessions.

    Does this make sense?

    Again I really appreciate the help.

    Thanks

    Kevin

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    how many items is irrelevant as long as you keep pushing them onto an array. But you do need some sort of input to let the user decide when they want to continue adding items - hardcoding the function calls in the main script will make them run once and putting them inside other functions will leave you with an annoying endless loop. A button would be logical, but there may be something on your "main menu" that would work as well.

    Old Pedant's code from the other thread, slightly reworked...
    Code:
    <html>
    <head>
    </head>
    <body>
    <input type="button" value = "add new user" onclick="userinfo()">
    <script>
    var info = new Array ();
    
    function User( n, s, fav)
    {
        this.nam = n;
        this.sur = s;
        this.favCol = fav;
        this.toString = function() {
               return "\nname: " + this.nam 
                    + ", surname: " + this.sur 
                    + ", favourite colour: " + this.favCol; };
    }
    
    
    function userinfo () {
    name = prompt ("Please enter your name.");
    surname = prompt ("Please enter your surname.");
    favouriteColour = prompt ("Please enter your favourite colour.");
    info.push (new User(name, surname, favouriteColour))
    alert(info)
    }
    
    </script>
    </body>
    </html>

  • Users who have thanked xelawho for this post:

    kev3kev3 (12-13-2011)

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi thanks for that,

    only thing is that the alert statement doesn't seem to work?

  • #6
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry it works.

    IT WORKS.

    Thanks so much, i know this is very simple, but I've been looking at it for days

    THANKS AGAIN


  •  

    Posting Permissions

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