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

时间:2021-09-06 22:10:44

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, i.e.:

我想要的是用标签替换两个提交按钮,即:

<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请求。

4 个解决方案

#1


1  

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
    • 发送表格

  • 在触发器中:更改操作URL发送表单

You should also register a trigger on form validation, and call one of the two trigger defined previously.

您还应该在表单验证上注册触发器,并调用之前定义的两个触发器之一。

#2


0  

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? if the reason is styling I am sure you know you can style that with just a class="myClass"

有多种方法可以这样做,首先你的代码应该可以工作,因为按钮应该默认提交,如果没有你可以做

#3


0  

Clicking the buttons will submit the form.

单击按钮将提交表单。

If you want to distinguish which button was clicked, give them names and values, just as you did in the first example.

如果要区分单击了哪个按钮,请为它们指定名称和值,就像在第一个示例中一样。

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

#4


0  

Yes by default the button submits the form. If this does not happen just use

是,默认情况下,该按钮提交表单。如果这不发生只是使用

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

#1


1  

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
    • 发送表格

  • 在触发器中:更改操作URL发送表单

You should also register a trigger on form validation, and call one of the two trigger defined previously.

您还应该在表单验证上注册触发器,并调用之前定义的两个触发器之一。

#2


0  

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? if the reason is styling I am sure you know you can style that with just a class="myClass"

有多种方法可以这样做,首先你的代码应该可以工作,因为按钮应该默认提交,如果没有你可以做

#3


0  

Clicking the buttons will submit the form.

单击按钮将提交表单。

If you want to distinguish which button was clicked, give them names and values, just as you did in the first example.

如果要区分单击了哪个按钮,请为它们指定名称和值,就像在第一个示例中一样。

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

#4


0  

Yes by default the button submits the form. If this does not happen just use

是,默认情况下,该按钮提交表单。如果这不发生只是使用

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