简体   繁体   English

如何使用HTML按钮标签发送HTML表单POST请求?

[英]How to send an HTML form POST request using the HTML button tag?

As of now, I have a user-input structure where the user provides their username/password then clicks either "login" or register" which then ends a post request with that information. 到目前为止,我有一个用户输入结构,其中用户提供其用户名/密码,然后单击“登录”或“注册”,然后以该信息结束发布请求。

<form action="{% url 'users:login' %}" method = "post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="passwd"><br>
    <input type="submit" name="login" value="Log In">
    <input type="submit" name="adduser" value="Add User">
</form>

What I want is to replace the two submit buttons with tags, ie: 我想要的是用标签替换两个提交按钮,即:

<form class="control-group" action="{% url 'users:login' %}" method = "post">
     <input type="text" placeholder="Username" name="username">
     <input type="password" placeholder="Password" name="passwd">
     <button class="button-style">Login</button>
     <button class="button-style">Register</button>
 </form>

But I'm unsure how to, for example, send the form's POST request with the correct information (username, password, type (login or register)) as I do in the first example. 但是,我不确定如何像在第一个示例中一样,以正确的信息(用户名,密码,类型(登录或注册))发送表单的POST请求。

You can use a bit a javascript. 您可以使用一些javascript。

Here is what you have to do : 这是您要做的:

  • Register a trigger on each button 在每个按钮上注册一个触发器
  • in the triggers : 在触发器中:
    • Change the action url 更改操作网址
    • Send the form 发送表格

You should also register a trigger on form validation, and call one of the two trigger defined previously. 您还应该在表单验证中注册一个触发器,并调用先前定义的两个触发器之一。

there are multiple ways to do so, first your code should work, because the button should submit by default, if not you can do <button type="submit">login</button> , if still does not work, you can use an onclick="myfunction()" and let the function handle the data, but may I ask why do you want to change it? 这样做有多种方法,首先您的代码应该可以工作,因为默认情况下该按钮应该提交,否则您可以执行<button type="submit">login</button> ,如果仍然无法工作,则可以使用onclick="myfunction()"并让函数处理数据,但是请问为什么要更改它? if the reason is styling I am sure you know you can style that with just a class="myClass" 如果原因是样式,我相信您知道可以仅使用class="myClass"样式设置

Clicking the buttons will submit the form. 单击按钮将提交表单。

If you want to distinguish which button was clicked, give them name s and value s, just as you did in the first example. 如果要区分单击了哪个按钮,请像在第一个示例中一样,给它们name s和value

<button class="button-style" name="login" value="Log In">Login</button>

Yes by default the button submits the form. 是,默认情况下,按钮提交表单。 If this does not happen just use 如果没有发生,请使用

<button type="submit">Login</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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