简体   繁体   中英

submit form from a link

This is what I am trying to do without success :

<form name="something" action="ht.php" method="post">
   <a href="#" onclick="document.url.submit('helloworld');">Submit</a>
</form>

When I click on the link I want to post the value helloworld to ht.php . How can I do this?

You can't just do document.url.submit(), it doesn't work like that.

Try this:

<form id="myForm" action="ht.php" method="post">
    <input type="hidden" name="someName" value="helloworld" />
    <a href="#" onclick="document.getElementById('myForm').submit();">Submit</a>
</form>

That should work!

Using jQuery, its rather easy:

$('form .submit-link').on({
    click: function (event) {
        event.preventDefault();
        $(this).closest('form').submit();
    }
});

Then you just code as normal, assigning the class submit-link to the form submission links:

<form action="script.php" method="post">
    <input type="text" name="textField" />
    <input type="hidden" name="hiddenField" value="foo" />
    <a href="#" class="submit-link">Submit</a>
</form>

I find this method useful, if you want to maintain an aesthetic theme across the site using links rather than traditional buttons, since there's no inline scripting.

Here's a JSFiddle , although it doesn't submit anywhere.

Try this,

<!-- you need to give some name to hidden value [index for post value] -->
<form name="something" action="ht.php" method="post">
    <input type="hidden" name="somename" value="helloworld" />
    <a href="javascript: document.something.submit();">Submit</a>
</form>

Also try this

<!-- you need to give some name to hidden value [index for post value] -->
<!-- also you can use id to select the form -->
<form name="something" action="ht.php" method="post" id="myform">
    <input type="hidden" name="somename" value="helloworld" />
    <a href="javascript: document.getElementById('myform').submit();">Submit</a>
</form>

try

<form id="frmMain" action="ht.php" method="post">
    <a href="#" onclick="document.forms['frmMain'].submit();">Submit</a>
</form>

You could add a hidden field on the page (set it's name property), set it's value to helloworld.

Then in your hyperlink's onclick call form.submit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM