467,885 Members | 1,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

document.submit() not work now?

I have a PHP application which I wrote last year - and the JavaScript
worked fine then. For some reason, now it doesn't - neither on IE nor
Firefox. Has something changed?

When I click on my HTML link now (which executes a JS function), the
firefox JS console tells me:
Error: document.SubjectsForm.submit is not a function

Any help would be much appreciated. Thanks!

The script, fairly explanatory, is as follows (it's been trimmed of
irrelevant code):
<html>
<head>
<title>Title here</title>
</head>

<body bgcolor="#FFFFFF" background="/images/bg.gif">

<SCRIPT language="JavaScript" type="text/javascript">
<!-- Hide script from old browsers
function submitForm() {
document.SubjectsForm.status.value='add_entry';
document.SubjectsForm.submit();
}
// unhide script -->
</SCRIPT>
<form name="SubjectsForm" action="mod_enrolment.php" method="POST">
<input type="hidden" name="status" value="ch_exams">

<a href="javascript: submitForm();" class="noline"> Add Subject</a>
<input type="hidden" name="user" value="tparker">
<input type="submit" name="submit" value="Update Subjects">

</form></p>

</body>
</html>

Jul 23 '05 #1
  • viewed: 33538
Share:
5 Replies
te************@gmail.com wrote:
I have a PHP application which I wrote last year - and the JavaScript
worked fine then. For some reason, now it doesn't - neither on IE nor
Firefox. Has something changed?

When I click on my HTML link now (which executes a JS function), the
firefox JS console tells me:
Error: document.SubjectsForm.submit is not a function
You have named your submit button "submit", so your reference above is
to the button, not the submit method of the form. Remove the button's
name or change it.

Any help would be much appreciated. Thanks!

The script, fairly explanatory, is as follows (it's been trimmed of
irrelevant code):
<html>
<head>
<title>Title here</title>
</head>

<body bgcolor="#FFFFFF" background="/images/bg.gif">
bgcolor and background are both depreciated, use the equivalent style
properties, preferably in a css style element.

<SCRIPT language="JavaScript" type="text/javascript">
The language attribute is depreciated, remove it. Keep the type
attribute, it is required.
<!-- Hide script from old browsers
This is pointless and potentially harmful - do not attempt to hide
scripts.
function submitForm() {
document.SubjectsForm.status.value='add_entry';
document.SubjectsForm.submit();
}
// unhide script -->
</SCRIPT>
<form name="SubjectsForm" action="mod_enrolment.php" method="POST">
<input type="hidden" name="status" value="ch_exams">

<a href="javascript: submitForm();" class="noline"> Add Subject</a>
Subverting an A element this way is bad. There is no need for the
javascript pseudo-protocol, the href should contain a URI that links to
an appropriate resource for non-javascript browsers. The script should
be assigned to an onclick event:

<a href="whyThisDidntWork.html" onclick="submitForm();" ...>...</a>

The purpose of the link seems completely unnecessary. Why not call
submitForm() from the form's onsubmit event? Then you have no issues
with links that don't work or the usability issue associated with a
link that attempts to submit a form. And the script will be called
regardless of how the form is submitted (provided javascript support is
available to the user).

Which probably also infers a change of name to 'changeStatus' or
similar and removal of the call to the form's submit method.

You should always assume that your script may not have run before the
form is submitted.
<input type="hidden" name="user" value="tparker">
<input type="submit" name="submit" value="Update Subjects">
Change the name or remove it (it seems superfluous anyway).

</form></p>
Closing 'P' tag that has no opening tag. Its placement infers that the
opening tag was before the form, but a P element can't contain a
block-level element (such as a form).

</body>
</html>

--
Rob
Jul 23 '05 #2
RobG wrote:
You have named your submit button "submit", so your reference above is
to the button, not the submit method of the form. Remove the button's
name or change it.
Thanks for the help Rob - you've managed to solve my problem. This was
the main problem, as you identified, which leaves me scratching my head
as to why it worked before or (more likely), when I made a change to
the code without even realising I had changed something.

I'm definitely not a JavaScript person - nor fancy HTML either. I'm
more of a PHP guy. I detest dreamweaver or most WYSIWYG programs since
their code is absolutely AWFUL. And doing everything by hand, it's a
pain in the arse to delve into complicated stuff like layers and such -
which half the time aren't necessary anyways. What happened to the days
when things were nice and simple?

bgcolor and background are both depreciated, use the equivalent style
properties, preferably in a css style element.
- as with the above, <body> would do fine for me until the day it is no
longer supported. I can't say i'm a fan of CSS and most of what I want
to develop doesn't need it.

<SCRIPT language="JavaScript" type="text/javascript">


The language attribute is depreciated, remove it. Keep the type
attribute, it is required.


I'll take your advice here. But is there any particular reason for
this? I notice most of the free JavaScripts include the opening tags
like this.

<!-- Hide script from old browsers


- They also (a lot of them) include a bit which ignores the script. Was
there any traditional reason for this before?

Subverting an A element this way is bad. -snip-
<a href="whyThisDidntWork.html" onclick="submitForm();" ...>...</a>
Oops. Actually what I used was:

<a href=\"javascript: void(0);\" onclick=\"submitForm();return
false;\">

But out of desperation when things didn't work I started playing around
with some changes, and must have used the wrong source code in my
posting.

Is javascript: void(0) acceptable? Or you would still suggest a real
link? Making the link without going running the function is as good as
not running it at all... so i'm not sure it would make a difference for
compatibility reasons.

The purpose of the link seems completely unnecessary. Why not call
submitForm() from the form's onsubmit event?


I need to run SubmitForm() becuase I want to add a hidden form variable
to the form before I submit it. The standard form submit button I have
(which would submit without this variable) achieves something else when
the resulting PHP page is loaded. Is there a better way to do this
though?

What I really need then is for two different ways to submit the form,
but one with an extra variable attached. Also I would prefer to use a
link since it looks nicer in my page.
Thanks again for your help.

Terence

Jul 23 '05 #3
te************@gmail.com wrote:
RobG wrote:
[...]
bgcolor and background are both depreciated, use the equivalent style
properties, preferably in a css style element.

- as with the above, <body> would do fine for me until the day it is no
longer supported. I can't say i'm a fan of CSS and most of what I want
to develop doesn't need it.


CSS is much cleaner and simpler, learn to use it and you'll be
surprised how you got along without it.

<SCRIPT language="JavaScript" type="text/javascript">
The language attribute is depreciated, remove it. Keep the type
attribute, it is required.

I'll take your advice here. But is there any particular reason for
this?


There is absolutely no need for it as far as I know, but one of the
gurus may be lurking and fill in on the history.
I notice most of the free JavaScripts include the opening tags
like this.
Most probably because either the authors couldn't be bothered to fix
their code or they don't know any better.

<!-- Hide script from old browsers

- They also (a lot of them) include a bit which ignores the script. Was
there any traditional reason for this before?


Way back level 2 browsers didn't know how to deal with script elements,
which were introduced with HTML 3.2. It is extremely unlikely anyone
is still surfing the web with Netscape 2 (or earlier) and if they are,
displaying the code from script elements is likely the least of their
worries.


Subverting an A element this way is bad. -snip-
<a href="whyThisDidntWork.html" onclick="submitForm();" ...>...</a>

Oops. Actually what I used was:

<a href=\"javascript: void(0);\" onclick=\"submitForm();return
false;\">

But out of desperation when things didn't work I started playing around
with some changes, and must have used the wrong source code in my
posting.


The backslashes before the quotes are unnecessary.

Is javascript: void(0) acceptable? Or you would still suggest a real
link? Making the link without going running the function is as good as
not running it at all... so i'm not sure it would make a difference for
compatibility reasons.
That is the reason why A elements shouldn't be turned into buttons.
Users without JavaScript will expect something to happen, but nothing
does. You are left in a quandary as to whether to navigate to a "you
don't have JavaScript page" or just let your visitor think your site is
broken.
The purpose of the link seems completely unnecessary. Why not call
submitForm() from the form's onsubmit event?

I need to run SubmitForm() becuase I want to add a hidden form variable
to the form before I submit it. The standard form submit button I have
(which would submit without this variable) achieves something else when
the resulting PHP page is loaded. Is there a better way to do this
though?


Why not hide the A by default, and only reveal it if JavaScript is
supported? Then non-JS users won't try to click it.

<a id="XX" href="#" onclick="submitForm();return false;"
style="display: none;">

<script type="text/javascript">
if ( document.getElementById ) {
document.getElementById('XX').style.display = '';
} else if ( document.all ) {
document.all['XX'].style.display = '';
}
</script>

Or if you have many links to expose, you may want to use an onload
function to reveal all of them. If you want to maintain the link's
position in the page even when it isn't shown, use visibility:
hidden/visible instead of display.

What I really need then is for two different ways to submit the form,
but one with an extra variable attached. Also I would prefer to use a
link since it looks nicer in my page.


Using the above strategy, users without JS will only ever see the
submit button and so will only use that.

Non-JS users will never have the extra field added.

Hope that helps!
--
Rob
Jul 23 '05 #4
te************@gmail.com wrote:
RobG wrote: [...]
<!-- Hide script from old browsers

- They also (a lot of them) include a bit which ignores the script. Was
there any traditional reason for this before?


I knew this was here but took a while to find it:

<URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/1d19a74dfc73fec6/c2e324e15908a9b0?q=hide+script&rnum=18&hl=en#c2e32 4e15908a9b0>

--
RobG
Jul 23 '05 #5
Thanks again for the help - will try out the suggestions.

So far i've got the submit link to work the way I want it to now - but
i'll be gradually improving the site, and it's probably about time I
learned JavaScript too - since PHP can only get me so far. But
anyways... thanks again for the help! Much appreciated.

Terence

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by skubik | last post: by
4 posts views Thread by jiing.deng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.