HTML,使按钮将数据提交到另一个网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16434725/
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
HTML, making a button submit data to another url?
提问by
The following is my HTML:
以下是我的 HTML:
<form class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn btn-success">Sign in</button>
<button class="btn btn-primary"> Make a new account </button>
</div>
</div>
</form>
Note: Just in case someone is wondering, it is utilizing the bootstrap library.
注意:以防万一有人想知道,它正在使用引导程序库。
I have the form and it POSTs to login.php but I want the button Make a new account
post the samedata to register.php.
我有表单,它发布到 login.php,但我希望按钮Make a new account
将相同的数据发布到 register.php。
Is that possible? If so, how?
那可能吗?如果是这样,如何?
采纳答案by filipko
Set action
attribute of your form
tag to the URL of the page to send the data to.
将标签的action
属性设置form
为要将数据发送到的页面的 URL。
回答by Vivek Jain
Try the following:
请尝试以下操作:
In HTML add an onclick event handler for Make a new account
button:
在 HTML 中为Make a new account
按钮添加一个 onclick 事件处理程序:
<form class="form-horizontal" method="post" id="myform">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn btn-success">Sign in</button>
<button class="btn btn-primary" onclick="javascript:register()"> Make a new account </button>
</div>
</div>
</form>
In JavaScript:
在 JavaScript 中:
function register() {
this.action = 'register.php';
return true;
}
If this does not work, you may try:
如果这不起作用,您可以尝试:
function register() {
var frm = document.getElementById("myform");
frm.action = 'register.php';
return true;
}
One or both of the above mentioned solution should work for you. Prefer the first approach as it is cleaner.
上述解决方案中的一种或两种都应该适合您。更喜欢第一种方法,因为它更干净。
Please take this as a starting point not a copy-paste solution and follow the best practices for development.
请将此作为起点而不是复制粘贴解决方案,并遵循最佳开发实践。
回答by CBroe
I have the form and it POSTs to login.php but I want the button Make a new account post the same data to register.php.
Is that possible?
我有表单并且它发布到 login.php 但我想要按钮创建一个新帐户将相同的数据发布到 register.php。
那可能吗?
In HTML5 it is, using the formactionattribute on the submit button.
在 HTML5 中,使用提交按钮上的formaction属性。
But browser support is probably not so good yet. Could be resolved using a Polyfil that attaches click handlers to such buttons automatically and changes the action
attribute of the form dynamically, though.
但是浏览器支持可能还不是很好。不过,可以使用 Polyfil 来解决,该 Polyfil 自动将单击处理程序附加到此类按钮并action
动态更改表单的属性。
回答by Arnaud Denoyelle
<form class="form-horizontal" method="post" action="register.php">