Click to See Complete Forum and Search --> : script for a phone book
kesonsdelight
10-13-2003, 09:36 AM
I have to make a phone book in javascript and I am having a really hard time getting it to work. I have been able to make the actual form but am unable to figure out how to get the button to work and to validate the input. Following is what I have to have:
1. phone list itself has to be stored as multi dimensional array
2. separate functions for validating input, adding to list and displaying list
3.user should be able to enter first name, last name and phone and press add button, this adds entry to phone book and displays new phone list.
*so when add fname, lname and phone it is supposed to display in text area.
*attached is what I have
Any help will be greatly appreciated...thankyou
Phil Karras
10-13-2003, 02:09 PM
You've only just begun....
First, you can not save data using JavaScript on any browser except IE.
IE has something called ActiveX, or something like that, which allows one to read and save client files.
So, while you can add to the array, as soon as you close down the browser you've lost your phone book.
As to checking the indputs from the user, well, you really have much to do, here's a start:
<html>
<head>
<title>Telephone Book</title>
<script language="JavaScript" type="text/javascript"> <!--
function CheckMe() {
document.PhoneBook.Book.value = document.PhoneBook.Book.value +
document.PhoneBook.firstName.value + " - " +
document.PhoneBook.lastName.value + " - " +
document.PhoneBook.phone.value + "\n";
alert(document.PhoneBook.firstName.value + " - " +
document.PhoneBook.lastName.value + " - " +
document.PhoneBook.phone.value + "\n");
return false;
}
// --> </script>
</head>
<body>
<form Name='PhoneBook' onSubmit='return CheckMe();'> <!-- pk -->
.
.
.
<input type="submit" value="Add Entry"></input> <!-- pk -->
.
.
.
<textarea Name='Book' rows="15" cols="60"></textarea> <!-- pk -->
.
.
.
</table>
</form> <!- pk -->
<script language='JavaScript' type="text/javascript">
var info = new Array(); //was: var info = newArray(); pk
.
.
.
</body>
</html>
Now at least you'll see the alert box pop up with the values entered. You now have to do three things:
1. check that the values are valid, in whatever way you want
2. save the values to the array
3. display them in the 'Book' textarea (I show a simple way to do this but you'll want to use some form
of for() statement to add all the values you've saved in the arrays.
Note the changes I made adding <form...> </form> and you forgot your <body> tag as well.
I've tested these additions out & they work - as far as they go.
good-luck
Charles
10-13-2003, 09:02 PM
Originally posted by Phil Karras
[A]nd you forgot your <body> tag as well.The start and end tags are optional for the BODY element as they are for the HTML, HEAD and TBODY elements. A valid DOCTYPE and a character set declaration, however, are required.
kesonsdelight
10-16-2003, 05:03 PM
I just wanted to thank you for your help. I am still unable to get this to work, JavaScript is really hard for me to understand. I am wondering would it be easier to use parallel arrays(3-firstName, lastName, and phone)? Then I need to figure out how to get it all to show in textarea when submitted.
Any help with this would be greatly appreciated.
Phil Karras
10-16-2003, 07:53 PM
It really doesn't matter how you do the date, three arrays, or an array of
a data structure holding thee values.
You need to start simply and just learn as you go to get the elements to
work the way you want them to.
I'd first start by learning about how to get data from and into form fields. The
textarea is a form field, so give it a name and you should be able to get data
into it as I've showed how to do it.
As to JavaScript being difficult, I'd say it's very similar to C, PHP, Perl, etc.
I'll bet you're making it far more difficult that it really is. (I tend to do that
when I don't "know" a language also.)
A very good book that will giver just about everything you'll need is:
Teach Yourself JavaScript in 24 Hours by Michael Moncur which is up to the
third edition now.
He also has a website for beginners: http://www.jsworkshop.com/