user1187135
user1187135

Reputation:

How do you submit a form using <a>

I tried the suggestion such as submit();, but it doesn't work for me... Can someone help.

 <section class="section_bkg" id="login">
    <form action="login.php" method="post" id="submit_login">
        <ul>
            <li>
                <label class="block" for="email">Email</label>
                <input type="email" name="email" id="email"/>
            </li>
            <li>
                <label class="block" for="password">Password</label>
                <input type="password" name="password" id="password"/>
            </li>
            <li><a href="#" onclick="document.form['submit_login'].submit();">Log in</a></li>
        </ul>
    </form>
</section>

Upvotes: 0

Views: 144

Answers (5)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201588

<a href="#" onclick=
 "document.getElementById('submit_login').submit(); return false">Log in</a>

But it’s really more useable and more accessible to use a submit button, so it is better to fix whatever issues you have with styling, rather than implement basic functionality this way.

Upvotes: 0

Vinoth Kumar
Vinoth Kumar

Reputation: 93

Here Answer

 <form action="login.php" method="post" id="myform" name="myform">
 </form>
 <a href="javascript:document.myform.submit();">Click to submit the form</a>

Upvotes: 0

Frankline
Frankline

Reputation: 41005

JavaScript provides the form object that contains the submit() method. Use the ‘id’ of the form to get the form object. For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:

document.forms["myform"].submit();

But, how to identify a form? Give an id attribute in the form tag

<form id='myform' action='handle-data.php'>

Here is the code to submit a form when a hyperlink is clicked:

<form name="myform" method="post" action="handle-data.php">
Search: <input type='text' name='query' />
<a href="javascript: submitform()">Search</a>
</form>
<script type="text/javascript">
function submitform()
{
  document.myform.submit();
}
</script>

Upvotes: 0

Kane
Kane

Reputation: 16802

Building on @dbaseman's answer. If you are using jQuery to submit the form this should help.

Html

<a href="#" id="MySubmitButton" name="MySubmitButton">Click to save</a>

jQuery

// submit
$('#MySubmitButton').click(function () {
    $('form').submit();
    return false;
});

// OR - bypassing any unbotrusive validation
$('#MySubmitButton').click(function () {
    $('form')[0].submit();
    return false;
});

// using the enter key to submit the form
$('#password, #email').keypress(function (event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if (keycode == 13) {
        $('form').submit();

        event.stopPropagation();
    }
});

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102753

First give the form an id attribute so you can reference it:

<form action="login.php" method="post" id="myform">

Now you can do a Javascript-submit, using the onclick event:

<a href="#" onclick="document.forms['myform'].submit();">Click to submit the form</a>

Upvotes: 2

Related Questions