Javascript 如何使用 PHP 提交 HTML 表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5189570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:04:26  来源:igfitidea点击:

How To Submit An HTML Form Using PHP?

javascripthtmlforms

提问by donvitto

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 helloworldto ht.php. How can I do this?

当我点击链接时,我想将值发布helloworldht.php. 我怎样才能做到这一点?

回答by Flipper

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

你不能只做 document.url.submit(),它不是那样工作的。

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!

那应该工作!

回答by Dan Lugg

Using jQuery, its rather easy:

使用 jQuery,它相当简单:

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

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

然后您只需像往常一样编码,将类分配给submit-link表单提交链接:

<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.

这是一个 JSFiddle,尽管它没有在任何地方提交。

回答by Santosh Linkha

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>

回答by White Dragon

try

尝试

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

回答by Jon

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

您可以在页面上添加一个隐藏字段(设置其名称属性),将其值设置为 helloworld。

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

然后在你的超链接的 onclick 调用 form.submit()