开发者

How To Submit An HTML Form Using PHP?

This is what I am trying to do without success :

<form name="something" action="ht.php" method="post">
   <a href="#" onclick="document.url.submit('hell开发者_如何学运维oworld');">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

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


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>


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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜