473,625 Members | 3,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing form elements in variables & arrays

I am trying to wrap my brain around storing form elements inside
variables & arrays before I move on to a more complicated project. I
created this simple example to experiment and as far as I can tell, it
should work but it doesn't. Can someone tell me where I went wrong?

<html><head>
<title>Form Test</title>
<script language="JavaS cript>
<!--
function copy(){
var firstField = new Array();
var secondField = new Array();

for(j=0; j<=4; j++){
firstField[j] = document.myForm .numText;
}

for(i=0; i<=4; i++){
secondField[i] = document.myForm .copyText;
}

firstField[0].value = "mark";
firstField[1].value = "hannon";
firstField[2].value = "anne";
firstField[3].value = "mulligan";

for(k=0; k<=4; k++){
secondField[k].value = firstField[k].value;
document.myForm .iValue.value = k;
}
}
// -->
</script>
</head>
<body bgcolor="#FFFFF F">

<form id="myForm" name="myForm" action="" method="POST"
enctype="text/plain">

<input name="numText" type="text" value="" size="15" maxlength="15"> <br>
<input name="copyText" type="text" value="" size="15" maxlength="15"
<br>

<input name="iValue" type="text" value="" size="2" maxlength="2" ><br>
value of i: <input name="copyBut" type="button" value="Copy"
onClick="copy() ">
</form>
</body>
</html>
Jul 23 '05 #1
2 2022
On Thu, 02 Sep 2004 05:24:33 GMT, Mark Hannon <ha*******@opto nline.net>
wrote:
I am trying to wrap my brain around storing form elements inside
variables & arrays before I move on to a more complicated project. I
created this simple example to experiment and as far as I can tell, it
should work but it doesn't.
What exactly do you expect the script to do? Its purpose seems unclear to
me, but it might be exactly what you intended.
Can someone tell me where I went wrong?
You didn't check your HTML. :)

It always pays to check the validity of your HTML before asking for help,
especially when scripts are involved. You can use the W3C's validator for
this:

<URL:http://validator.w3.or g/>

There is just one syntax error that has crippled the entire page, but I'll
suggest several changes.
<html>
Valid HTML pages should contain document type declaration. See:

<URL:http://www.w3.org/TR/html4/struct/global.html#h-7.2>

for more information.
<head>
<title>Form Test</title>
<script language="JavaS cript>
This is the problem. You omitted the closing quote. A good
syntax-highlighting editor would have shown this. However, whilst we're
here, you should replace the language attribute with the type attribute.
The former is deprecated whilst the latter is required.

<script type="text/javascript">
<!--
Script hiding is obsolete. There are no browsers in use today that will
render the contents of a script, even if they no ability to execute
scripts. If you really are worried about content rendering, move the
script into an external file and use the src attribute. This should be
done for production sites, anyway.
function copy(){
var firstField = new Array();
var secondField = new Array();
It tends to be simpler to use array literal for declaring arrays:

var firstField = [],
secondField = [];

If you want to initialise arrays with this syntax, you simply place the
values within the brackets separated by spaces.

anArray = ['my', 'initialised', 'array', 2004];
for(j=0; j<=4; j++){
firstField[j] = document.myForm .numText;
}
Just as you declared the first- and secondField variables, you should do
the same for 'j', above. Using the var keyword prevents variables from
becoming global.

for(var j = 0; j <= 4; ++j) {
// ...
}

The same applies to the other two counters used. However, as you restart
each counter from zero, you might as well use the one variable.

Another improvement can be used when you access the same property
repeatedly.

var form = document.forms['myForm'];

This means that from now on, you can use the form identifier to access the
form object.

More information about this, and the property accessor I introduced above,
can be found in the group FAQ (and the accompanying notes):

<URL:http://jibbering.com/faq/#FAQ4_13>

[snip]
// -->
Be sure to remove this with the other comment delimiter.
</script>
</head>
<body bgcolor="#FFFFF F">


The bgcolor attribute is deprecated. All new HTML documents should really
be using the Strict DTD and CSS instead of presentational attributes and
elements.

[snip]

If you have other problems, remember to do two things:

1) Check your HTML
2) Read the FAQ

One of those might solve the issue without even having to ask for help.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Thanks for the help Mike,

It was late when I posted that message and I had been working on this
and tweaking it for a few hours. As is often the case, the longer you
stare at and labor over something the less you are able to see the
problems you have created for yourself. I'll take your advice on the
validation, array declaration and JavaScript tags.

Mark

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4436
by: Nomen Nescio | last post by:
can anyone be so kind as to look at http://www.mysolution.ws/HYPOCRITE.php and let me know why it isn't passing the form data to http://www.mysolution.ws/insertHYPOCRITES.php for the most part, the scripts were created with http://phpcodegenie.sourceforge.net/
1
1420
by: nt9 | last post by:
I am very new with php so excuse me please if the question seems really easy.I have a page(form) which displays a list of the titles of some books(retreived from a database) and each title has a selection box with option values 1 to 7 each.The user should be able to enter 7 choices of his favourite books and store them in the database at a table called preferences with fields: customer_id ,first_choice, sec_choice,third_choice etc.I am...
3
11753
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
6
2563
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
3
1808
by: Kay | last post by:
ABC ASF DFS ASS ABC <--- Duplicate JJK I want to store above char in an arry, but I'm not sure I'm right or not anyone can guide me or give me some suggestions, Thx?
14
4935
by: Stainless | last post by:
I have a public class Globals, which obviously holds all my global data. I have an array of 243 items, currently structs of type typedef struct STAR { int x; int y; int stellar_class; }t_star;
22
2534
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple' account and submitted them under that, but this makes it harder to print lists of all the reviews by one person, so ideally I wanna make a multiple select form so we can just select all the contributors and have the values saved in the database - in...
7
1919
by: eric.goforth | last post by:
Hello, I'm working with a classic asp page that calls another classic asp page. The html in my calling page looks like: <form method="post" action="/includes/mypage2.asp?subtype=MyType&amp;ecd=1&amp;eid=97702"> ....Do some stuff
2
7043
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
0
8192
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
8696
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
8637
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...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
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.