document.submit() not work now?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • terence.parker@gmail.com

    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.Subjec tsForm.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="#FFFFF F" background="/images/bg.gif">

    <SCRIPT language="JavaS cript" type="text/javascript">
    <!-- Hide script from old browsers
    function submitForm() {
    document.Subjec tsForm.status.v alue='add_entry ';
    document.Subjec tsForm.submit() ;
    }
    // unhide script -->
    </SCRIPT>


    <form name="SubjectsF orm" action="mod_enr olment.php" method="POST">
    <input type="hidden" name="status" value="ch_exams ">

    <a href="javascrip t: 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>

  • RobG

    #2
    Re: document.submit () not work now?

    terence.parker@ gmail.com wrote:[color=blue]
    > 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.Subjec tsForm.submit is not a function[/color]

    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.
    [color=blue]
    >
    > 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="#FFFFF F" background="/images/bg.gif">[/color]

    bgcolor and background are both depreciated, use the equivalent style
    properties, preferably in a css style element.
    [color=blue]
    >
    > <SCRIPT language="JavaS cript" type="text/javascript">[/color]

    The language attribute is depreciated, remove it. Keep the type
    attribute, it is required.
    [color=blue]
    > <!-- Hide script from old browsers[/color]

    This is pointless and potentially harmful - do not attempt to hide
    scripts.
    [color=blue]
    > function submitForm() {
    > document.Subjec tsForm.status.v alue='add_entry ';
    > document.Subjec tsForm.submit() ;
    > }
    > // unhide script -->
    > </SCRIPT>
    >
    >
    > <form name="SubjectsF orm" action="mod_enr olment.php" method="POST">
    > <input type="hidden" name="status" value="ch_exams ">
    >
    > <a href="javascrip t: submitForm();" class="noline"> Add Subject</a>[/color]

    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="whyThisDi dntWork.html" onclick="submit Form();" ...>...</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.
    [color=blue]
    > <input type="hidden" name="user" value="tparker" >
    > <input type="submit" name="submit" value="Update Subjects">[/color]

    Change the name or remove it (it seems superfluous anyway).
    [color=blue]
    >
    > </form></p>[/color]

    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).
    [color=blue]
    >
    > </body>
    > </html>
    >[/color]


    --
    Rob

    Comment

    • terence.parker@gmail.com

      #3
      Re: document.submit () not work now?

      RobG wrote:[color=blue]
      > 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.[/color]

      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?

      [color=blue]
      > bgcolor and background are both depreciated, use the equivalent style
      > properties, preferably in a css style element.[/color]

      - 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.
      [color=blue][color=green]
      > >
      > > <SCRIPT language="JavaS cript" type="text/javascript">[/color]
      >
      > The language attribute is depreciated, remove it. Keep the type
      > attribute, it is required.[/color]

      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.

      [color=blue][color=green]
      > > <!-- Hide script from old browsers[/color][/color]

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

      [color=blue]
      > Subverting an A element this way is bad. -snip-
      > <a href="whyThisDi dntWork.html" onclick="submit Form();" ...>...</a>[/color]

      Oops. Actually what I used was:

      <a href=\"javascri pt: void(0);\" onclick=\"submi tForm();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.

      [color=blue]
      > The purpose of the link seems completely unnecessary. Why not call
      > submitForm() from the form's onsubmit event?[/color]

      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

      Comment

      • RobG

        #4
        Re: document.submit () not work now?

        terence.parker@ gmail.com wrote:[color=blue]
        > RobG wrote:
        >[/color]
        [...][color=blue][color=green]
        >> bgcolor and background are both depreciated, use the equivalent style
        >> properties, preferably in a css style element.[/color]
        >
        >
        > - 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.[/color]

        CSS is much cleaner and simpler, learn to use it and you'll be
        surprised how you got along without it.
        [color=blue]
        >
        >[color=green][color=darkred]
        >>><SCRIPT language="JavaS cript" type="text/javascript">[/color]
        >>
        >> The language attribute is depreciated, remove it. Keep the type
        >> attribute, it is required.[/color]
        >
        >
        > I'll take your advice here. But is there any particular reason for
        > this?[/color]

        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.
        [color=blue]
        > I notice most of the free JavaScripts include the opening tags
        > like this.
        >[/color]
        Most probably because either the authors couldn't be bothered to fix
        their code or they don't know any better.

        [color=blue][color=green][color=darkred]
        >>> <!-- Hide script from old browsers[/color][/color]
        >
        >
        > - They also (a lot of them) include a bit which ignores the script. Was
        > there any traditional reason for this before?[/color]

        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.
        [color=blue]
        >
        >
        >[color=green]
        >> Subverting an A element this way is bad. -snip-
        >> <a href="whyThisDi dntWork.html" onclick="submit Form();" ...>...</a>[/color]
        >
        >
        > Oops. Actually what I used was:
        >
        > <a href=\"javascri pt: void(0);\" onclick=\"submi tForm();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.[/color]

        The backslashes before the quotes are unnecessary.
        [color=blue]
        >
        > 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.[/color]

        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.
        [color=blue]
        >[color=green]
        >> The purpose of the link seems completely unnecessary. Why not call
        >> submitForm() from the form's onsubmit event?[/color]
        >
        >
        > 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?[/color]

        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="submit Form();return false;"
        style="display: none;">

        <script type="text/javascript">
        if ( document.getEle mentById ) {
        document.getEle mentById('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.
        [color=blue]
        >
        > 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.[/color]

        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

        Comment

        • RobG

          #5
          Re: document.submit () not work now?

          terence.parker@ gmail.com wrote:[color=blue]
          > RobG wrote:[/color]
          [...][color=blue]
          >[color=green][color=darkred]
          >>> <!-- Hide script from old browsers[/color][/color]
          >
          >
          > - They also (a lot of them) include a bit which ignores the script. Was
          > there any traditional reason for this before?
          >[/color]

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

          <URL:http://groups.google.c om.au/group/comp.lang.javas cript/browse_frm/thread/1d19a74dfc73fec 6/c2e324e15908a9b 0?q=hide+script &rnum=18&hl=en# c2e324e15908a9b 0>





          --
          RobG

          Comment

          • terence.parker@gmail.com

            #6
            Re: document.submit () not work now?

            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

            Comment

            Working...