Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 Posts

    form submission outside the form

    Is it possible to submit a form via a link or button if that link or button exists outside the form? If so, I would think it required javascript.

    Code:
    <form name="input" action="html_form_action.asp" method="get">
    Username: <input type="text" name="user">
    ...
    <a name="save">&nbsp;</a>
    ...
    <input type="submit" value="Submit">
    </form>
    ...
    Click here to <a href="#save">SAVE</a> the contents of the form
    ...
    At the moment all that happens (obviously) is that clicking on the word SAVE goes to the bookmark above the Submit button.

    Is there a way to have the click do the submit instead?
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    <a href="#save">SAVE</a>
    If you could depend on javascript, then you may write it like
    Code:
    <form id="input" action="html_form_action.asp" method="get">
    Code:
    <a href="#" onclick="document.forms['input'].submit();">SAVE</a>
    Why can't you use a normal submit button include it in the form tag?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    tpeck (04-13-2013)

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 Posts
    That's really helpful - thank you!

    I already have a submit button in the form but I want to be able to also submit the same form from elsewhere. It's difficult to describe exactly why without seeing the page!
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by abduraooft View Post
    Code:
    <form id="input" action="html_form_action.asp" method="get">
    That part's right but you then didn't make use of it properly to actually submit the form. The correct way rto reference that is as follows.

    Code:
    document.getElementById(input').submit();
    document.forms['input'] refers to <form name="input>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    <form name="input" action="html_form_action.asp" method="get">
    Username: <input type="text" name="user">
    ...
    <a name="save">&nbsp;</a>
    ...
    <input type="submit" value="Submit" />
    <input type="submit" value="Save" onclick="this.form.action='somethingelse.asp';" />
    </form>


  •  

    Posting Permissions

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