Results 1 to 5 of 5

Thread: this.form.submit() not working, but maybe it shouldn't..

  1. #1

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    this.form.submit() not working, but maybe it shouldn't..

    Is using the onclick="this.form.submit();" method of form submission restricted to use only on other <input /> tags?

    Is there a way to use this method on an <a> tag?

    I'm trying to achieve something like this, but it's not working:
    Code:
    <form action="serverFile.php" method="post">
    <!-- various input controls -->
    <a onclick="this.form.submit();">Send</a>
    </form>
    I'm trying to find the least 'hardwired' manner of doing this, because yes, I am aware of:

    Code:
    <form name="myForm" action="serverFile.php" method="post">
    <!-- various input controls -->
    <a href="javascript:document.myForm.submit();">Send</a>
    </form>
    And I am trying not to use this if possible.
    IWS

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: this.form.submit() not working, but maybe it shouldn't..

    what's wrong with that second method?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: this.form.submit() not working, but maybe it shouldn't..

    I just don't want to use that method. I'm trying to keep all elements independent of one another. Dynamically generated forms, etc., etc.

    Most importantly, to see if it's possible.
    IWS

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: this.form.submit() not working, but maybe it shouldn't..

    This would achieve what you want:
    Code:
    <form action="serverFile.php" method="post">
    <!-- various input controls -->
    <a href="#" onclick="this.parentNode.submit();return false;">Send</a>
    </form>
    Anchor elements don't seem to "register" as a link without the href attribute, so add that. "this" refers to the anchor element in the above context, so to say "this.form" would refer to the form element within "this" - there exists no such thing. Using "this.parentNode" refers to the parent element of the anchor, which is the form. If you put the anchor within a div or something, then its parent would no longer be the form, and this wouldn't work. Also, you want to return false to the onclick event or else the browser will try to process the link as normal (it'll submit the form, but then go to "#" (reload the current page, in effect)).

    If you're really interested in unobtrusive Javascript, you should do two things: remove the inline script, as well as write the HTML with a standard submit input, and have the Javascript sniff out and replace that element on page load (this is so that if a user has Javascript disabled, they'll still be able to use the form with a normal submit button; if they have Javascript enabled, the link will replace the button).

  5. #5
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: this.form.submit() not working, but maybe it shouldn't..

    Code:
    <form id="form1" name="form1" method="post" action="file.php">
      <input type="text" name="textfield" id="textfield" />
    </form>
    <a href="#" onclick="document.form1.submit()">send</a>
    Last edited by fjober; Feb 14th, 2010 at 04:08 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width