By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,945 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,945 IT Pros & Developers. It's quick & easy.

SIMPLE PROGRAM: user enters number, gets personal message

P: n/a
kw
Hello,

I am trying to put up a page for my students for them to be able to enter
their class number into a field and instantly get a personalized text
message. (ie student number 5 enters "5" and gets a message like
"Congratulations on your homerun in Kickball Bobby!"

Can this be done using a form and if so would that be the best way to do it?
Any help would be greatly appreciated!
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
kw wrote:
Hello,

I am trying to put up a page for my students for them to be able to enter
their class number into a field and instantly get a personalized text
message. (ie student number 5 enters "5" and gets a message like
"Congratulations on your homerun in Kickball Bobby!"

Can this be done using a form and if so would that be the best way to do it?
Any help would be greatly appreciated!


<script type="text/javascript">
var customMessages = [
null,
'This is message one',
'This is message two',
'This is message three',
'This is message four',
'This is message five'
];
function setMessage(inp) {
var idx = +inp.value;
if (!isNaN(idx) && idx > 0 && customMessages[idx]) {
inp.form.output.value = customMessages[idx];
} else {
inp.form.output.value = '';
}
}
</script>
<form>
<input type="text" name="class_number" size="3" maxlength="3"
onchange="setMessage(this);" />
<br />
<input type="text" name="output" readonly="readonly" style="border:none;"
onfocus="this.blur();" tabindex="-1" />
</form>

The above is the simplest way to do it with the widest range of browser support.
<url: http://jibbering.com/faq/#FAQ4_15 /> provides a way to modify the content
of a <span> or <div> in an existing page if you feel you want to tackle that.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #2

P: n/a
Lee
kw said:

Hello,

I am trying to put up a page for my students for them to be able to enter
their class number into a field and instantly get a personalized text
message. (ie student number 5 enters "5" and gets a message like
"Congratulations on your homerun in Kickball Bobby!"

Can this be done using a form and if so would that be the best way to do it?
Any help would be greatly appreciated!


I would do it like this.
It's pretty easy to update, and you can have a personalized
greeting for every student without having to think of something
completely original for each one. Be careful about commas when
you make changes, and be sure to test it before your students do.
<html>
<head>
<script type="text/javascript">

msg = [

// Double-quotes around names and around messages.
// Any double-quotes inside the messages must be \"escaped\"
// You can use %name% anywhere in the message.
// It's ok to skip numbers that don't match any student.

1, "Abe", "Welcome to our school, Abe!",
2, "Jan", "Jan, Please say hello to your sister Marcia for me!",
3, "Peter", "Hello, %name%!",
5, "Bobby", "Congratulations on your homerun in kickball, Bobby!",
6, "Ted", "Hello, %name%!",
7, "Jim", "Hello, %name%!",
8, "Sue", "Hello, %name%!",
9, "Bob", "Hello, %name%!",
10, "Joe", "Hello, %name%!",
12, "Kim", "Hi, %name%. I've always liked the name, \"%name%\"!",
14, "Zoe", "Hi, %name%. I've always liked the name, \"%name%\"!"

// No comma at the end of the last message!
// When you add names to the end, remember to add a
// comma to the message that used to be last.

];

function greet(id){
id=id.replace(/\s*/g,""); // remove any accidental spaces
for(var i=0;i<msg.length;i+=3){
if(msg[i]==id){
var greeting=msg[i+2].replace(/%name%/g,msg[i+1]);
alert(greeting);
return;
}
}
alert("I think you made a mistake.\n"+
"I don't know student number \""+id+"\".");
}
</script>
</head>
<body>
<form onsubmit="return false"> <!-- don't really submit this form to the server
-->
Type your student number here: <input name="id" size="4"><br>
Then press the "Hello" button: <input type="button"
value="Hello"
onclick="greet(this.form.id.value)">
</form>
</body>
</html>

Jul 23 '05 #3

P: n/a
kw
"Lee" <RE**************@cox.net> wrote in message
news:c8*********@drn.newsguy.com...
kw said:

Hello,

I am trying to put up a page for my students for them to be able to enter
their class number into a field and instantly get a personalized text
message. (ie student number 5 enters "5" and gets a message like
"Congratulations on your homerun in Kickball Bobby!"

Can this be done using a form and if so would that be the best way to do it?Any help would be greatly appreciated!
I would do it like this.
It's pretty easy to update, and you can have a personalized
greeting for every student without having to think of something
completely original for each one. Be careful about commas when
you make changes, and be sure to test it before your students do.
<html>
<head>
<script type="text/javascript">

msg = [

// Double-quotes around names and around messages.
// Any double-quotes inside the messages must be \"escaped\"
// You can use %name% anywhere in the message.
// It's ok to skip numbers that don't match any student.

1, "Abe", "Welcome to our school, Abe!",
2, "Jan", "Jan, Please say hello to your sister Marcia for

me!", 3, "Peter", "Hello, %name%!",
5, "Bobby", "Congratulations on your homerun in kickball, Bobby!", 6, "Ted", "Hello, %name%!",
7, "Jim", "Hello, %name%!",
8, "Sue", "Hello, %name%!",
9, "Bob", "Hello, %name%!",
10, "Joe", "Hello, %name%!",
12, "Kim", "Hi, %name%. I've always liked the name, \"%name%\"!", 14, "Zoe", "Hi, %name%. I've always liked the name, \"%name%\"!"
// No comma at the end of the last message!
// When you add names to the end, remember to add a
// comma to the message that used to be last.

];

function greet(id){
id=id.replace(/\s*/g,""); // remove any accidental spaces
for(var i=0;i<msg.length;i+=3){
if(msg[i]==id){
var greeting=msg[i+2].replace(/%name%/g,msg[i+1]);
alert(greeting);
return;
}
}
alert("I think you made a mistake.\n"+
"I don't know student number \""+id+"\".");
}
</script>
</head>
<body>
<form onsubmit="return false"> <!-- don't really submit this form to the server -->
Type your student number here: <input name="id" size="4"><br>
Then press the "Hello" button: <input type="button"
value="Hello"
onclick="greet(this.form.id.value)">
</form>
</body>
</html>

That works perfectly. Thank you so much I really appreciate it!

(Thank you too Grant!)
Jul 23 '05 #4

P: n/a
In article <c8*********@drn.newsguy.com>, Lee
<RE**************@cox.net> wrote:

<snip>
1, "Abe", "Welcome to our school, Abe!",
2, "Jan", "Jan, Please say hello to your sister Marcia for me!",
3, "Peter", "Hello, %name%!",
5, "Bobby", "Congratulations on your homerun in kickball, Bobby!",
6, "Ted", "Hello, %name%!",
7, "Jim", "Hello, %name%!",
8, "Sue", "Hello, %name%!",
9, "Bob", "Hello, %name%!",
10, "Joe", "Hello, %name%!",
12, "Kim", "Hi, %name%. I've always liked the name, \"%name%\"!",
14, "Zoe", "Hi, %name%. I've always liked the name, \"%name%\"!"

<snip>

Since the messages are unique to each student number, why separate out
the name? Your idea is great if there was a set of messages and the
message number is used in each entry.

message[1] = "Hello, %name%!"

6, "Ted", 1,
etc.

--
Dennis Marks
http://www.dcs-chico.com/~denmarks/
To reply change none to dcsi.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #5

P: n/a
Lee
Dennis Marks said:

In article <c8*********@drn.newsguy.com>, Lee
<RE**************@cox.net> wrote:

<snip>
1, "Abe", "Welcome to our school, Abe!",
2, "Jan", "Jan, Please say hello to your sister Marcia for me!",
3, "Peter", "Hello, %name%!",
5, "Bobby", "Congratulations on your homerun in kickball, Bobby!",
6, "Ted", "Hello, %name%!",
7, "Jim", "Hello, %name%!",
8, "Sue", "Hello, %name%!",
9, "Bob", "Hello, %name%!",
10, "Joe", "Hello, %name%!",
12, "Kim", "Hi, %name%. I've always liked the name, \"%name%\"!",
14, "Zoe", "Hi, %name%. I've always liked the name, \"%name%\"!"

<snip>

Since the messages are unique to each student number, why separate out
the name? Your idea is great if there was a set of messages and the
message number is used in each entry.

message[1] = "Hello, %name%!"

6, "Ted", 1,


Ease of maintenance is one of the most important factors in
this design. I specifically wanted the message to be typed
right next to the student's name, despite the redundancy.

You don't want to accidentally congratulate the wrong student
because you got their numbers mixed up.

An alternative that I considered was to have an array of
constructor calls, (or even literal Objects) such as:

msg = [
new Greeting(1, "Bobby", "Congratulations!"),
new Greeting(3, "Sue"),
new Greeting(4, "Jim")
]

Which would make it easier to have a default greeting, but it
seemed like it was adding more sources of error.

Note that in either design, you could define some standard messages
and replace the literal string messages with variable names:

var default = "Hello, %name%!";
var tardy = "Late again, %name%?";

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.