simple js dynamically reading form values - js fails - why?

R

Randell D.

HELP!

I am determined to stick with this... I'm getting there... for those who
haven't read my earlier posts, I'm createing what should be a simple
function that I can call to check that required fields in a form have
values. I'm writing the values to the client using document.write only so
that I can confirm that the values are there to be played with - this is not
the final result so please (unless it's leading to the script failure)
please ignore layout - I don't need to hear about the <br> or CSS or
anything else that looks nice - I'm working on basic program flow at the
moment.... Oh! and note I'm a javascript newbie comming - I have some
progarmming experience with PHP which I would have thought would have helped
me along a bit...

So... Can someone help my next step? In my script below, it draws a basic
form - The theory behind it is once values have been written in to the two
form fields and the user presses the submit button, I would expect to have
the form values re-gurgitated (?) back on to the screen (again, just to
prove that the values are visable inside firstFunction()

I want the script to be dynamic - Thus, from an onclick event on the submit
button, the function is called and passed the form name, a required form
field (as named inside the "<INPUT>" html tag), followed by the 'real name'
of the tag that the user gets to see in the browser. Thus... the idea being
that if the input tag 'firstname' has no value, an alert box will popup
asking for a value for "First Name" and not "firstname'.

My problem?

If you examine the script, variable xyz gets assigned the firstname entered
in the form - This is proven (ie displayed using document.write).

The next step is to create a loop that would read the supplied arguements to
read values from the form. Thus... my example would be form fields
'firstname' and 'lastname'...

What is done are the arguements passed to the function are displayed (these
are the form field tag names). And when I try to use these values (see
variable tmpe in the script) to display form field values, the script
exits - no errors - nothing. When I exclude 'tmpe' from the script, the
script completes by looping twice (since the function was passed two field
names)... but I cannot use the form field names dynamically within the
loop... why?

All help, via the newsgroup (share+help others) is much appreciated, thanks
randelld

My script (also at http://www6.wiredwords.net/test/e.html)

<body>
<script type="text/javascript">
function firstFunction(formName)
{
// Read in arguements passed to the function from the onClick event in the
forms submit button
var argv = firstFunction.arguments;
var argc = firstFunction.arguments.length;
// The document.write below *did* work earlier - Now the script exits when I
include it - why?
// document.write("argc = " + argc);

// Can we read anything from the form? Try a hard code reading of a form
value
var xyz=document.forms[formName].elements['firstname'].value;
document.write(" xyz = " + xyz);

// Simplify our form data object name in to formData
var formData=document.forms[formName];

// Define tmpe - for our test purposes, we assign a dash as a value just so
that we know
// it has a value
var tmpe="-";

// Loop through the numbre of arguements passed to firstFunction() - Read
from the second arguement
// since the first arguement is formName
for(count=1; count<argc; count=count+2)
{ // Simplify our form field name in to a single variable
fieldName=argv[count];
// Again, for the hell of it, confirm fieldName is valid by making
note of its length
tmpb=fieldName.length;
// tmpc is the user friendly name of a field - thus it has a value of
First Name as opposed
// to the html tag name firstname
tmpc=argv[count+1];
// Dump our values to the screen, one per line to make things easier
to read.
document.write("<hr>count=" + count + "<hr>fieldName=" + fieldName
+ "<hr>tmpb=" + tmpb + "<hr>tmpc = " + tmpc );

// I have tried the following methods to define a value for tmpe but
they fail -
// What is the correct syntax and if possible, why does it fail?
// tmpe=formData.elements[fieldName].value;
// tmpe=document.forms[formName].elements[fieldName].value;
// tmpe=document.forms[formName].elements['firstname'].value;
document.write("<hr>tmpe=" + tmpe);
// Brace below closes the for loop
}

// Brace below closes the function
}

</script>

<form name="myForm">
<br>Firstname: <input type=text name=firstname size=30> Lastname: <input
type=text name=lastname size=30>
<input type="submit" onclick="firstFunction('myForm','lastname','Last
Name','firstname','First Name'); return">
<hr><a href="./e.html">again</a><hr>
</form>
<p><hr></p>
</body>





THANKS!
 
R

Randell D.

I got it... in a previous post I had been told not to use document.write and
use alert() instead - I was told that using document.write was ineffiicient
but I had not realised that it was causing my problems...

I've written my scrpit exactly the way I'd like it to work - thanks to all
who helped...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top