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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.forms[0].submit() doesn't work

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
0
0
NL
I don't get this:

I've got a form (name="toets_2") and this button:
<input type="button" name="end" value="END" onClick=\"document.toets_2.submit();>

But whenever I want to submit with this button I get this error: Object doesn't support this property or method

Why? This (or document.forms[0].submit()) used to work fine!

Any suggestions? Is it IE? Mozilla1.0.1 doesn't work either...

Don't eat yellow snow!
 
I'm not sure if you were just giving me psuedo-code, but you have a missing double-quote and a superfluous backslash. Try this:

[tt]
<input type="button" name="end" value="END" onClick="document.toets_2.submit();">
[/tt]

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
Thanks.... but I gave you all bad code, my mistake. Never the less.... This still does not work:

Complete code (PHP):

echo "<input type=\"button\" name=\"end\" value=\"END\" onClick=\"document.toets_2.submit();\">\n";


Don't eat yellow snow!
 
Is there any reason you're not using a SUBMIT button?

[tt]echo "<input type=\"submit\" name=\"end\" value=\"END\">\n";[/tt]

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
The button is an addition to the submit-button.
I also get the same error-massage whenever I try to submit the form from another frame with: parent.mainFrame.document.toets_2.submit();

Basicly: the javascript-function document.form.submit() doesn't seem to work! And I've used it succesfully before...

Don't eat yellow snow!
 
And document.forms['toets_2'].submit(); doesn't work either? Are you sure of the capitalization? Could you post the relevant html?

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
This is the important part of the script (partly PHP):

<body onLoad="document.toets_2.elements[0].focus();">
<form method="post" action="verwerk.php" name="toets_2" id="toets_2" autoComplete="off">
Here some formfields....
<input type="submit" name="submit" value="Verder" onKeypress="swallowEnter()">
</form>
<?
if (empty($_COOKIE['toets_einde']))
{
echo "<input type=\"button\" name=\"end\" value=\"EINDE\" onClick=\"document.forms['toets_2'].submit(); location.href='theEnd.php'\"";
}
?>
</body>

Any idea?

Don't eat yellow snow!
 
By submitting the form, you're sending the form data to the verwerk.php page. However, before this has a chance, you're setting the location.href value to theEnd.php. Without actually trying this code, I'd say that what is happening is when you click the button, the page changes to theEnd.php. Then you check for data, that should exist, and it doesn't. Is that correct?

What I suggest you do is, in the verwerk.php page, have the script do whatever it needs to do with the data, not print anything out to the browser, then use php's header function to redirect the user to theEnd.php. It is called like this:

[tt]header("Location: theEnd.php");[/tt]

You cannot call this function if any html (even spaces) have been written to the browser, so make sure you follow that rule.

Does this help?

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
Unfortunately the location.href doesn't get executed because the first function (the document.forms['toets_2'].submit()) isn't working... It makes no difference, with or without the location.href the submit() does not work.

Sigh....

Don't eat yellow snow!
 
Tests I performed:

1) I made a simple form with one text box. I had one submit button inside the form, and a regular button outside the form with document.the_form.submit(); in the onclick event.
Result: successful.

2) I then moved the regular button to inside the </form> tag and tried each button again.
Result: successful.

3) I then made the regular button a SUBMIT button.
Result: successful.

4) I then moved the new submit button outside the </form> tag.
Result: failure - the second submit button did not submit the form, since it was outside the form.

The only suggestion I have left is putting this button inside the </form> tag, maybe it'll help.

If not, can you please post the exact html that PHP is generating?

Thanks...

*cLFlaVA
----------------------------
Ham and Eggs walks into a bar and asks, "Can I have a beer please?"
The bartender replies, "I'm sorry, we don't serve breakfast.
 
The functionality of the javascript submit-function should be that you can submit a form from outside the form (or frame) itself? Otherwise: why develop something like parent.mainFrame.document.forms['toets_2'].submit()?

Could it be a security-configuration of my machine? (XP Pro with Norton Internet Security 2004)


Don't eat yellow snow!
 
Post the relevant HTML generated by the PHP. I (virtually) guarantee an answer from this forum if you do that.

--Dave
 
Quasibobo,

make sure you do not have a form element named "submit" in your form, or you will have a naming collision and the script will fail

e.g.

<input name="submit" />



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
I do have a <input type="submit"> in the form... I'll change it to <input type="button" onClick="submit()">. If that does not work, I'll get back to you all.

Thanks so far!

Don't eat yellow snow!
 
an <input type="submit"> is ok, as long as it does not have a "name" of "submit" also. any other form element named "submit" will cause the problem too.




=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
That's the problem! I replaced the submit-button with a <input type="button" onClick="submit()"> and now everything works ok!

Thanks

Don't eat yellow snow!
 
The problem is that you cannot have any form field named be "submit". It behaves like an invalid name, or a reserved word. You can use it, but the parser will be looking for a form field named "submit" instead of a function "submit()". It is a stupid thing. The parser should be able to notice the parens at the end. But somehow this confuses it. The same problem happens if you name a field "reset" and then you try to call the reset() function on the form.

Your button is working now. But if you named the button "submit" it would break just like before. Notice the type is a button:
<input type="button" name="submit" value="Verder" onKeypress="swallowEnter()">

You can change your code back to using a submit button. Just don't name the button. Or use something other than "submit".

The general rule of thumb is you have no need to name your submit button, unless you have multiple submit buttons. Then you use something other than "submit".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top