Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript function to post form... And more

Status
Not open for further replies.

mikemedia

Programmer
Apr 28, 2006
39
US
Real basic multiple choice quiz in a <form>

I have a button: type="button" which works well calling my function sumScore() and filling results into text fields.

I also have the standard form "submit" button for the
<form method="post" action="doscore.asp">

I want to combine the two into one button.

I've tried including the following into my function:
document.form.method="post"
document.form.action="doscore.asp"
 
Code:
function test() {
	document.test_form.submit();
}

<form name="test_form" method="POST" action="[URL unfurl="true"]http://domain.com/something.cgi">[/URL]

...

</form>

M. Brooks
 
Nope! I already tried that. Good guess though.

document.myform.submit();

results in an error:
'document.myform' is null or not an object

And yes my form is indeed
<form id="myform" method= "post" action="doscore.asp">
 
If your form has an ID, why not refer to it by its ID?:

Code:
document.getElementById('myform')

To answer your question, to combine the 'filler' code and the submission, you could call the code 'onsubmit':

Code:
<form ... onsubmit="sumScore();" ...>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
'document.myform' is null or not an object

Place the javascript after the <form> or do...
Code:
function test() {
    document.getElementById('myform').submit();
}

<form name="test_form" method="POST" action="[URL unfurl="true"]http://domain.com/something.cgi">[/URL]

M. Brooks
 
mbrooks

see here:
Code:
function test() {
    document.getElementBy[!]Id[/!]('myform').submit();
}

<form [!]name[/!]="test_form" method="POST" action="[URL unfurl="true"]http://domain.com/something.cgi">[/URL]

[monkey][snake] <.
 
Oopsie! That's what happens when you don't pay attention when 'cutting & pasting'.

Correction..
Code:
function test() {
    document.getElementById('test_form').submit();
}

<form id="test_form" method="POST" action="[URL unfurl="true"]http://domain.com/something.cgi">[/URL]

M. Brooks
 
Thanks guys!
We're closin' in on it!

I'm still not reaching the 'doScore.asp'.

Here's what's not happening:
//My Submit calls the JS function

<form onsubmit="sumScore();" name="myform" method= "post" action="doscore.asp">

// the function contains various stuff for calculations(all of which test ok by itself) and write to a text field
None of the fields fill in though.

var total= a + b + c
var grade = total * 33
document.getElementById('totalScore').value = grade;

//Finally we submit the form but the address reads:

I know it's Friday. So everybody take a break. If you feel inspired, I'll be checking in regularly. Thanks!
 
Can you show us the code for the buttons? Both the one that calls the "sumScore()" and the "submit".

Are you giving your buttons names and/or ids? I got bit once by naming my submit button "submit". That kept me from scripting the submit() function for my form. Nasty thing to debug.

Einstein47
There are no kangaroos in Austria!
[&#91;]Starbase47.com]
 
CODE URL:

I appreciate everyone's help.

I've commented out lines: 117 and portions of 128
Lines 108 - 116 truly puzzle me. They should work.

The ultimate goal is to fill three fields (date, totalScore, and passFail) AND write the values to a database.

I'll be most intrigued by any positive resolution.

My thanks again.
 
I think that code should be,

--> document.simpleQuiz.getElementById
to
--> document.getElementById

then the else is missing "}{"

Code:
if(document.simpleQuiz.getElementById('totalScore').value < 50){
	document.simpleQuiz.getElementById('passFail').value = "Failed";
		[COLOR=#ff0000]}[/color]else[COLOR=#ff0000]{[/color]
	document.simpleQuiz.getElementById('passFail').value = "Passed";
  }

might want to var sumScore too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top