473,763 Members | 3,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SIMPLE PROGRAM: user enters number, gets personal message

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
"Congratulation s 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
5 2221
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
"Congratulation s 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_num ber" size="3" maxlength="3"
onchange="setMe ssage(this);" />
<br />
<input type="text" name="output" readonly="reado nly" style="border:n one;"
onfocus="this.b lur();" 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*****@agrico reunited.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
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
"Congratulatio ns 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", "Congratulation s 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.lengt h;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="retur n 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.va lue)">
</form>
</body>
</html>

Jul 23 '05 #3
kw
"Lee" <RE************ **@cox.net> wrote in message
news:c8******** *@drn.newsguy.c om...
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
"Congratulatio ns 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", "Congratulation s 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.lengt h;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="retur n 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.va lue)">
</form>
</body>
</html>

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

(Thank you too Grant!)
Jul 23 '05 #4
In article <c8*********@dr n.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", "Congratulation s 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
Lee
Dennis Marks said:

In article <c8*********@dr n.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", "Congratulation s 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", "Congratulation s!"),
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
14346
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
6
4964
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help...
7
4122
by: jmac | last post by:
Greetings fellow programmers, I have created a C program that has a few bugs and would like to get some help with working them out. Here is a list of the problems that I am experiencing: - The program calls for the input to be added in one line (? 100 Smith 24.98), but my right now my program skips a line each time for example: ? 100
3
5212
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
14
12742
by: Just starting out | last post by:
I am very new to C code and I'm having a lot of trouble with a homework assignment. This program is supposed to take the amount of Euros that the user enters and convert it to US dollars. It runs fine if the user enters a number, but if the user enters a letter it loops. I have been working on this for 3 hours now, trying different things
7
2183
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the "continue_function", and if NO is returned to "get_data", display_report executes and the program ends? Basically I am having trouble understanding the program flow within the "if" loop in the "main" function.
4
3108
by: cnoobie | last post by:
Problem 1 - Min, Mean, Max Write a program that reads in successive integer values from the user. The user will indicate termination of these values with the sentinel value 0 (zero). After the program has read in the values, the program will output the minimum value entered, the maximum value entered, and the mean (average) of the values entered (to two decimal places). If the user does not enter any numbers before entering 0, the program...
1
2518
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to start it and what main topics in C I will need to cover in the assignment. Thanks a lot. CSC180 Assignment #3: Menu Madness Due Date: Thursday, November 8th at 1:00am Contents: · General Info · What to Hand In o Submission Instructions
5
9369
by: just curious | last post by:
Create a C++ console application that uses a while loop to count, total, and average a series of positive integers entered by a user. The user enters a –1 to signal the end of data input and to display the count, total, and average of the numbers entered. The pseudocode for the program is: Start Enter user's input Begin Loop
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9992
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3519
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.