Submitting A Form

Discussion in 'Javascript' started by Super Steve, Nov 2, 2006.

  1. Super Steve

    Super Steve Guest

    I'm trying to write a little script that disables the submit button on
    a form, changes the text of the button, and then submits the form.

    Can anyone tell me why this works ok:

    <input type="button" value="Submit" onClick="this.disabled=true;
    this.value = 'Submitting...'; this.form.submit();">

    But when I try to make a function it doesn't work:

    <script language="JavaScript">
    function DisableButton(b) {
    b.disabled = true;
    b.value = 'Submitting...';
    b.form.submit();
    }
    </script>

    <input type="button" value="Submit" name="submit"
    onClick="DisableButton(this);">

    When I try this the button text is changed and the button is disabled,
    but the form doesn't submit. In IE I get an error that points to the
    line that says:

    b.form.submit();

    and the error:

    Object doesn't support this property or method

    What's the difference between doing this code directly in the onClick
    event and doing it in a function?
     
    Super Steve, Nov 2, 2006
    #1
    1. Advertising

  2. Super Steve

    Guest

    because b=to the button not to the form the riht syntax should be:

    window.document.FormName.submit();



    > b.form.submit();
    >
    > and the error:
    >
    > Object doesn't support this property or method
    >
    > What's the difference between doing this code directly in the onClick
    > event and doing it in a function?
     
    , Nov 2, 2006
    #2
    1. Advertising

  3. Super Steve wrote:
    > I'm trying to write a little script that disables the
    > submit button on a form, changes the text of the button,
    > and then submits the form.
    >
    > Can anyone tell me why this works ok:
    >
    > <input type="button" value="Submit" onClick="this.disabled=true;
    > this.value = 'Submitting...'; this.form.submit();">
    >
    > But when I try to make a function it doesn't work:
    >
    > <script language="JavaScript">
    > function DisableButton(b) {
    > b.disabled = true;
    > b.value = 'Submitting...';
    > b.form.submit();
    > }
    > </script>
    >
    > <input type="button" value="Submit" name="submit"
    > onClick="DisableButton(this);">
    >
    > When I try this the button text is changed and the button is
    > disabled, but the form doesn't submit. ...

    <snip>

    <URL: http://jibbering.com/faq/faq_notes/form_access.html#faComMis >

    Richard.
     
    Richard Cornford, Nov 2, 2006
    #3
  4. Super Steve

    Super Steve Guest

    Thanks for your reply. Initially I thought the same as you, but then I
    read that every form element also had a "form" property that points to
    the form that the element is an item of.

    "All form element objects also have the form property, which returns
    the Form object in which the element is contained. This can be useful
    in cases where you have a generic routine that checks the validity of
    data in a form. For example, when the user clicks a submit button, we
    can pass the Form object referenced by the form property to our data
    checker which can use it to loop through each element on the form in
    turn, checking that data in the element is valid. This is handy if you
    have more than one form defined on the page or where you have a generic
    data checker that you cut and paste to different pages - this way you
    don't need to know the form's name in advance."

    So I don't know why b.form.submit.() doesn't work when b is pointing to
    the button element within the form.


    wrote:
    > because b=to the button not to the form the riht syntax should be:
    >
    > window.document.FormName.submit();
    >
    >
    >
    > > b.form.submit();
    > >
    > > and the error:
    > >
    > > Object doesn't support this property or method
    > >
    > > What's the difference between doing this code directly in the onClick
    > > event and doing it in a function?
     
    Super Steve, Nov 2, 2006
    #4
  5. Super Steve

    Super Steve Guest

    Awsome, Richard!

    Well that's a trap for your players. I've always named my submit
    buttons "submit" in the past, but then I've never tried to access them
    with JavaScript before. Changing the name to "submitform" now makes
    everything work.

    Many thanks for your help!


    Richard Cornford wrote:

    > <URL: http://jibbering.com/faq/faq_notes/form_access.html#faComMis >
    >
    > Richard.
     
    Super Steve, Nov 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Brown

    Submitting form with a unique ID

    James Brown, Jul 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    387
    David Waz...
    Jul 6, 2003
  2. Mike Szanto
    Replies:
    1
    Views:
    521
  3. Dave Smithz
    Replies:
    3
    Views:
    34,716
    mokker
    Oct 30, 2009
  4. Marco Alting
    Replies:
    7
    Views:
    536
    Thomas 'PointedEars' Lahn
    Oct 30, 2003
  5. Kai Grossjohann
    Replies:
    4
    Views:
    287
Loading...

Share This Page