Off-topic Talk Where overpaid, underworked S2000 owners waste the worst part of their days before the drive home. This forum is for general chit chat and discussions not covered by the other off-topic forums.

Javascript experts?

Thread Tools
 
Old Nov 24, 2001 | 02:20 PM
  #1  
cthree's Avatar
Thread Starter
Administrator
20 Year Member
 
Joined: Oct 2000
Posts: 20,274
Likes: 4
From: Toronto, Canada
Default

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.
Reply
Old Nov 24, 2001 | 02:56 PM
  #2  
CRitchie's Avatar
Registered User
 
Joined: Apr 2001
Posts: 454
Likes: 0
From: Salisbury
Default

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.
Reply
Old Nov 24, 2001 | 03:25 PM
  #3  
robw01's Avatar
Registered User
 
Joined: Sep 2001
Posts: 179
Likes: 0
From: Cleveland
Default

...

<script language="javascript">
function submitForm(){
document.forms["formname"].submit()
//Or - document.forms[0].submit()
}
</script>

...

<form name="formname" action="someotherpage.asp">

Submit Me
</form>
Reply
Old Nov 24, 2001 | 03:47 PM
  #4  
ElTianti's Avatar
Registered User
 
Joined: May 2001
Posts: 2,997
Likes: 0
From: Rome, GA
Default

Yeah, I seem to remember that your form tag needs an action or
a submit won't happen. Next question are you sure you function is being called? Did you put in an alert to confirm this?
Reply
Old Nov 24, 2001 | 07:31 PM
  #5  
CRitchie's Avatar
Registered User
 
Joined: Apr 2001
Posts: 454
Likes: 0
From: Salisbury
Default

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.
Reply
Old Nov 24, 2001 | 09:45 PM
  #6  
cthree's Avatar
Thread Starter
Administrator
20 Year Member
 
Joined: Oct 2000
Posts: 20,274
Likes: 4
From: Toronto, Canada
Default

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") {

Reply
Old Nov 25, 2001 | 01:18 AM
  #7  
S2K Fan's Avatar
Registered User
 
Joined: Feb 2001
Posts: 6,898
Likes: 0
From: San Jose
Default

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>

Reply
Old Nov 25, 2001 | 07:35 AM
  #8  
robw01's Avatar
Registered User
 
Joined: Sep 2001
Posts: 179
Likes: 0
From: Cleveland
Default

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
You can use the onLoad event for the body tag. You can submit the form directly from there. Or, if there is processing that needs to be done before the form is submitted you can call a function from the onLoad event. Do the processing in the funcion and have the submit() method called from the function after the processing.

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.
Reply
Old Nov 25, 2001 | 07:46 AM
  #9  
CRitchie's Avatar
Registered User
 
Joined: Apr 2001
Posts: 454
Likes: 0
From: Salisbury
Default

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>
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
mingster
Off-topic Talk
19
Nov 4, 2004 07:27 PM
Jaykkub
Off-topic Talk
5
Jan 28, 2004 11:16 AM
ironwedge
Off-topic Talk
2
Jul 8, 2002 07:01 AM




All times are GMT -8. The time now is 02:19 PM.