Javascript experts?
can anyone answer the question of why this doesn't work? I get document.formname is null or not an object:
<form name="formname" ...>
...
</form>
<script>
document.formname.submit();
</script>
Same problem if I do:
document.forms[0].submit();
God I hate JavaScript
JavaScript: The incompatible, undebugable random behaviour language for all your needs.
<form name="formname" ...>
...
</form>
<script>
document.formname.submit();
</script>
Same problem if I do:
document.forms[0].submit();
God I hate JavaScript

JavaScript: The incompatible, undebugable random behaviour language for all your needs.
First, I would use the second version as I think it is more widely acceptable by browsers.
Second, in your script tag you may want to tell the browser what langauge the script is with <script language="Javascript">.
Third, if the script tries to run before the body of the html page is loaded then "formname" will not be set yet and therefore it will be null. Make sure the page is completely loaded before you start referencing the objects of the form.
Hope this helps.
Second, in your script tag you may want to tell the browser what langauge the script is with <script language="Javascript">.
Third, if the script tries to run before the body of the html page is loaded then "formname" will not be set yet and therefore it will be null. Make sure the page is completely loaded before you start referencing the objects of the form.
Hope this helps.
...
<script language="javascript">
function submitForm(){
document.forms["formname"].submit()
//Or - document.forms[0].submit()
}
</script>
...
<form name="formname" action="someotherpage.asp">
Submit Me
</form>
<script language="javascript">
function submitForm(){
document.forms["formname"].submit()
//Or - document.forms[0].submit()
}
</script>
...
<form name="formname" action="someotherpage.asp">
Submit Me
</form>
You can also do something like this:
<script language="javascript">
function submitForm(action) {
document.forms[0].action = "page1.asp?action=" + action;
document.forms[0].submit();
}
</script>
<form name="formname" method="post">
<input type="button" value="New" onclick="submitForm('new');">
<input type="button" value="Update" onclick="submitForm('update');">
</form>
This method allows you to pass different parameters to the page1.asp script. Notice that there is no action attribute in the form tag.
<script language="javascript">
function submitForm(action) {
document.forms[0].action = "page1.asp?action=" + action;
document.forms[0].submit();
}
</script>
<form name="formname" method="post">
<input type="button" value="New" onclick="submitForm('new');">
<input type="button" value="Update" onclick="submitForm('update');">
</form>
This method allows you to pass different parameters to the page1.asp script. Notice that there is no action attribute in the form tag.
The script follows the form and the form is complete with method and action. I simply removed it from the sample. Thus the form is loaded when the script is called.
<form name="formname" method="POST" action="someurl">
...
</form>
<script>
somefunction(document.formname);
</script>
works just fine. It's not a browser compability issue, it fails with both NS6 and IE6. Normally there is would be a page with a form and a submit button which the user clicks to submit the form. I want to bypass that user interaction and submit the form automatically once it loads. I don't need <script lang="JavaScript">, all browsers do JavaScript by default if lang is not specified.
I've worked around it by having the user manually press the submit button but that is an unnecessary extra step, that's why ideally I'd like to bypass it. I don't control the script in the action parameter (PayPal does).
Here is a more complete example:
[CODE]
<?PHP
if($radio == "a") {
<form name="formname" method="POST" action="someurl">
...
</form>
<script>
somefunction(document.formname);
</script>
works just fine. It's not a browser compability issue, it fails with both NS6 and IE6. Normally there is would be a page with a form and a submit button which the user clicks to submit the form. I want to bypass that user interaction and submit the form automatically once it loads. I don't need <script lang="JavaScript">, all browsers do JavaScript by default if lang is not specified.
I've worked around it by having the user manually press the submit button but that is an unnecessary extra step, that's why ideally I'd like to bypass it. I don't control the script in the action parameter (PayPal does).
Here is a more complete example:
[CODE]
<?PHP
if($radio == "a") {
So is problem solved?
I usually keep my submits inside my form. ie...
<form method="post" action="../form/web_messaging.cgi" name="web_messaging_form" ENCTYPE="application/x-www-form-urlencoded">
...
<a onClick="document.web_messaging_form.submit(); return false;" onMouseOver="window.status='Send Message'; document.SENDM.src='../img/SENDMbl.gif'; return true" onMouseOut="document.SENDM.src='../img/SENDMbw.gif'" style="cursor: hand">[img]../img/SENDMbw.gif[/img]</a>
...
</form>
I usually keep my submits inside my form. ie...
<form method="post" action="../form/web_messaging.cgi" name="web_messaging_form" ENCTYPE="application/x-www-form-urlencoded">
...
<a onClick="document.web_messaging_form.submit(); return false;" onMouseOver="window.status='Send Message'; document.SENDM.src='../img/SENDMbl.gif'; return true" onMouseOut="document.SENDM.src='../img/SENDMbw.gif'" style="cursor: hand">[img]../img/SENDMbw.gif[/img]</a>
...
</form>
Trending Topics
Originally posted by cthree
Normally there is would be a page with a form and a submit button which the user clicks to submit the form. I want to bypass that user interaction and submit the form automatically once it loads
Normally there is would be a page with a form and a submit button which the user clicks to submit the form. I want to bypass that user interaction and submit the form automatically once it loads
I use the following:
<body onload="javascript:document.forms[0].submit()">
-OR-
...
<script language=javascript>
function ProcessAndSubmit(){
//processing code here
//submit here
document.forms[0].submit()
}
</script>
...
<body onload="javascript:ProcessAndSubmit()">
...
BTW - There's obviously no space between "java" and "script", but I can't seem to get the post to quit putting one there.
The code example you give above works like a champ for me in all cases.
I did this however and it still worked.
<html>
<head>
</head>
<body>
<?php
if($radio == "a") {
// do something
echo("<form name=a method=post action=test.php>");
echo("<input type=hidden name=fname value=6>");
echo("</form>");
echo("<script>document.a.submit();</script>");
exit;
}
else if($radio == "b") {
// do something else
echo("Something Else");
exit;
}
?>
</body>
</html>
If I call this page (called helloworld.php) with the following:
someurl/helloworld?radio=a
I get the first if and it reports the actual hidden value fname.
If I call it with:
someurl/helloworld?radio=b
I get the else part.
Then for grins I took all the html tags out of helloworld.php and left just plain php code. That is I removed the <html><head></head><body></body></html> and it still worked ok.
Not sure why yours isn't working. I am running this in IE6.0. It even worked in NN4.
For completeness here is test.php:
<html>
<head>
</head>
<body>
<?php echo $fname ?>
</body>
</html>
I did this however and it still worked.
<html>
<head>
</head>
<body>
<?php
if($radio == "a") {
// do something
echo("<form name=a method=post action=test.php>");
echo("<input type=hidden name=fname value=6>");
echo("</form>");
echo("<script>document.a.submit();</script>");
exit;
}
else if($radio == "b") {
// do something else
echo("Something Else");
exit;
}
?>
</body>
</html>
If I call this page (called helloworld.php) with the following:
someurl/helloworld?radio=a
I get the first if and it reports the actual hidden value fname.
If I call it with:
someurl/helloworld?radio=b
I get the else part.
Then for grins I took all the html tags out of helloworld.php and left just plain php code. That is I removed the <html><head></head><body></body></html> and it still worked ok.
Not sure why yours isn't working. I am running this in IE6.0. It even worked in NN4.
For completeness here is test.php:
<html>
<head>
</head>
<body>
<?php echo $fname ?>
</body>
</html>
Thread
Thread Starter
Forum
Replies
Last Post




