简体   繁体   English

如何处理表单中的两个提交按钮?

[英]How to deal with two submit buttons in a form?

I don't understand this, 我不明白这个,

if I do this and I click the check out button , the page won't go to the check out page , 如果我这样做,我点击结账按钮 ,页面将不会进入结账页面

<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>

It only does if I change the name="checkout" to name="cart-checkout" , 只有当我将name="checkout"更改为name="cart-checkout"时才name="cart-checkout"

<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="cart-checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>

It works that way but does not make any sense to me, do you know why it does it that way? 它是这样工作但对我没有任何意义,你知道它为什么这样做吗?

So I tried to use <a> tag inside the <button> tag and it goes to the check out page, 所以我尝试在<button>标签内使用<a>标签,然后进入结账页面,

<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="checkout" id="checkout" type="submit" value="Check out"><a href="checkout.php">Check out</a></button>
</form>

But does it a valid html to put <a> tag inside the <button> tag? 但是将<a>标签放在<button>标签内是否有效

Why not change the buttons to: 为什么不将按钮更改为:

<input name="update" id="update" type="submit" value="Update cart">
<input name="cart-checkout" id="checkout" type="submit" value="Check out">

Then in PHP (on cart.php) you can do: 然后在PHP(在cart.php上)你可以做:

<?php
  if($_POST){
     if(isset($_POST['update']){
       // process update
     } else if(isset($_POST['cart-checkout']){
       // process cart checkout
       // or uncomment the below line to forward to checkout.php
       // header("Location: checkout.php");
     }
  }
?>

What about making one <button> that simply acts like a <a href=""> ? 如何制作一个像<a href=""> <button>一样的<button>呢?

-edit- -编辑-

Whoops, sorry, misread. 哎呀,抱歉,误读。 <button onclick="window.location='/nextpage';"> ? <button onclick="window.location='/nextpage';">

我实际上会在这里使用带有onclick事件的JavaScript,它会带你到结帐页面。

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

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