PDA

View Full Version : Using arrays to process multiple forms with the same name


jaydee
10-13-2002, 01:32 AM
Hello!

Any help here would be greatly appreciated!

I have a situation where I will have multiple occurences of the same form in a HTML document. (ie multiple forms on a page where the forms all have the same name and its elements have the same names as those on the other forms.)

When the user hits a button on one of the forms, a Javascript function is called to open another window and display the selected form elements in this window.

How do I use arrays to handle this?

I've included a cut down version of the type of form processing that I wish to use. This code will work for one instance of the form.

I'm confused about how I define an array for forms and access the elements in that array.

The code......

<html>
<head>
<script language="JavaScript">
<!--
function print()
{
msgBox=window.open("","displayWindow",menubar="yes",
scrollbars="yes",status="yes",width=200,height=200);
msgBox.document.write(document.demo.author.value)
msgBox.document.write(document.demo.title.value)
}

//-->
</script>

</head>
<body>

<form name="demo">
<input type="hidden" name="author" value="Smith J. Jones G.">
<input type="text" name="title" value="How Green is my Valley">

<input type="button" value="Details" onClick='print()'>
</form>

jaydee
10-13-2002, 02:28 AM
I've done some more surfing and figured out how to use forms and arrays in certain situations.

For example, I could change my code to do this....

<html>
<head>
<script language="JavaScript">
<!--
function print( n )
{
msgBox=window.open("","displayWindow",menubar="yes",
scrollbars="yes",status="yes",width=200,height=200);
msgBox.document.write(document.forms[n].author.value)
msgBox.document.write(document.forms[n].title.value)
}

//-->
</script>

</head>
<body>

<form name="demo">
<input type="hidden" name="author" value="Smith J. Jones G.">
<input type="text" name="title" value="How Green is my Valley">

<input type="button" value="Details" onClick='print( 0 )'>
</form>

<form name="demo">
<input type="hidden" name="author" value="Smith J. Jones G.">
<input type="text" name="title" value="How Green is my Valley">

<input type="button" value="Details" onClick='print( 1 )'>
</form>
.
.
......



But my problem is that my HTML code is being generated dynamically and I will not know when I am generating the code for each form, what element they will be in the array. So I can't hard code the subscript n.

Anyone have any bright ideas? Hope this post is clear ...been a long night!

adios
10-13-2002, 04:25 AM
First: print() is not a good name for a function - there already is a window.print()...


<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var HTML, msgBox = null;
function showdata() {
var form, f = 0, el, e;
if (msgBox && !msgBox.closed) msgBox.close();
HTML = '';
HTML += '<html><head><title>Data Window</title>';
HTML += '<style type="text/css">';
HTML += 'body{font:200 11px arial;color:ivory;background:olive;overflow:auto;}';
HTML += 'input{font:200 11px arial;color:olive;background:ivory;width:36px;}';
HTML += '</style></head><body><br><div style="font-size:14px;text-align:center;">';
HTML += '&amp;#149; The Details &amp;#149;</div><br><br>';
while (form = document.forms[f++]) {
e = 0;
while (el = form.elements[e++]) {
if (el.type != 'button')
HTML += '<b>' + el.name + ': </b>' + el.value + '<br>';
}
HTML += '<hr>';
}
HTML += '<div style="text-align:center;">';
HTML += '<form><input type="button" value="close" onclick="self.close()">';
HTML += '</form></div></body></html>';
msgBox = open('javascript:opener.HTML','displayWindow','left=300,top=150,width=180,height=250,scrollbars');
if (msgBox && !msgBox.closed) msgBox.focus();
}

</script>
</head>
<body bgcolor="beige">
<form name="demo">
<input type="hidden" name="author" value="Mr. jaydee">
<input type="text" name="title" size="22" value="How Green is Her Valley">
<input type="button" value="Details" onclick="showdata()">
</form>
<form name="demo">
<input type="hidden" name="author" value="Mrs. jaydee">
<input type="text" name="title" size="22" value="How Green is His Valley">
<input type="button" value="Details" onclick="showdata()">
</form>
</body>
</html>


Note: I'm getting sick of having to say this: remove the space in opener. HTML & put the arguments all on one line...
eeesh! put this on one line too:
olive;overflow:auto;}
';

Would someone out there fix this? Your editor is breaking peoples code.