Page 1 of 1

Javascript: collect data from a form for a function

Posted: Sun Sep 07, 2008 6:56 am
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.

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

Posted: Sun Sep 07, 2008 11:20 am
by andyhoneycutt
use an input type="button" instead of type="submit". your form is submitting which is why it's clearing.

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

Posted: Sun Sep 07, 2008 2:12 pm
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:

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

Posted: Tue Sep 09, 2008 5:22 am
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 :)

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

Posted: Fri Sep 12, 2008 2:50 pm
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! :)