View Full Version : trying to store user entered info please help

12-12-2011, 07:05 PM

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

Philip M
12-12-2011, 08:06 PM
Have a look at this recent thread which is in effect the same question:-


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

12-12-2011, 08:14 PM
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.



12-12-2011, 10:02 PM
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...

<input type="button" value = "add new user" onclick="userinfo()">
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))


12-13-2011, 09:58 AM
Hi thanks for that,

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

12-13-2011, 10:08 AM
Sorry it works.


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