Javascript: collect data from a form for a function

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
p3aul
Forum Newbie
Posts: 10
Joined: Fri Oct 18, 2002 10:30 pm

Javascript: collect data from a form for a function

Post by p3aul »

Hi!
I'm trying to collect data from a form to use in a function, but it doesn't seem to be working. I'm using Dreamweaver to write the code. Here is what I have. This is the JS function that I use.

Code: Select all

<script type="text/javascript">function printnum(){$ft = document.getElementById('db').valueDocument.Write($ft)}</script>
This is the code for the form. Please ignore the other text fields. They aren't used for now

Code: Select all

<form id="form1" name="form1" >  <label>  <input type="text" name="db" />    Dry Bulb Temp(Celsius)</label>  <p>    <label>    <input type="text" name="wb" />    Wet Bulb Temp(Celsius)</label>  </p>  <p>    <label>    <input type="text" name="pr" />    Ambient Pressure(Millibars)</label></p>  <p>    <label>     <input type="submit" name="Submit" value="Calculate" onclick="printnum(document.form1)"/>    </label>  </p></form>
When I use this form and click Calculate button, the form clears but the value I input in the textfield "db" isn't printed.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Javascript: collect data from a form for a function

Post by andyhoneycutt »

use an input type="button" instead of type="submit". your form is submitting which is why it's clearing.
p3aul
Forum Newbie
Posts: 10
Joined: Fri Oct 18, 2002 10:30 pm

Re: Javascript: collect data from a form for a function

Post by p3aul »

andyhoneycutt wrote:use an input type="button" instead of type="submit". your form is submitting which is why it's clearing.
Well that eliminated the clearing but I still don't get anything printed :banghead:
shaneiadt
Forum Newbie
Posts: 10
Joined: Sat Mar 15, 2008 9:26 pm

Re: Javascript: collect data from a form for a function

Post by shaneiadt »

You're getting the element by id in your js but you have no id specified in your input tag;

Code: Select all

[size=150]<input type="text" name="db" [b]id="db"[/b] />[/size]
This is needed if you want to get the element by id :)
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: Javascript: collect data from a form for a function

Post by BETA »

Well those are two errors... but i see many there...
JS is case sensitive if im not wrong this means "Document" is different from "document" so it won't work even if u do the changes shaneiadt told u. And "Write" is different from "write" too, so the correct way is document.write("whatever");
Another thing:

Code: Select all

onclick="printnum[b](document.form1)[/b]"
and

Code: Select all

function printnum[b]()[/b]
Im not sure about this since i don't know everything about all languages but it's wrong too i think... If im not wrong u can't send a variable to a function if the function doesn't have the variable defined in it's declaration... i mean if u want to do that u should use something like this:

Code: Select all

onclick="printnum[b](document.form1)[/b]"
and

Code: Select all

function printnum[b](obj)[/b]
And last thing... i recommend u to use a semicolon (;) for each statement u write... i think it's not necessary if u do a line jump but it's better.

Hope this helped! :)
Post Reply