Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > simple js dynamically reading form values - js fails - why?

Reply
Thread Tools

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

 
 
Randell D.
Guest
Posts: n/a
 
      11-24-2003

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!


 
Reply With Quote
 
 
 
 
Randell D.
Guest
Posts: n/a
 
      11-25-2003

"Randell D." <(E-Mail Removed)> wrote in message
newspuwb.488431$9l5.457359@pd7tw2no...


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...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IE7 fails on dynamically adding a <form> into a <div> timjowers Javascript 4 12-04-2008 03:10 PM
Re: Reading Multiple Values from List using JSP and Form Post Method Lew Java 0 02-09-2008 08:57 PM
How to retrieve values of dynamically added web controls on the form?? Aruna Bajpayee ASP .Net Building Controls 1 07-27-2004 09:23 PM
Reading Form values into a Scripting.Dictionary Tim Chmielewski ASP General 2 06-23-2004 10:08 PM
js onclick event to read values from form fails - why? Randell D. Javascript 7 11-25-2003 12:19 PM



Advertisments