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

发布于 2024-12-18 08:10:27 字数 1402 浏览 5 评论 0原文

我不明白这一点,

如果我这样做并单击结帐按钮,页面将不会转到结帐页面

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

只有当我将 name="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>

它是这样工作的,但对我来说没有任何意义,你知道为什么这样做吗那样?

所以我尝试在

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

但是它是有效的 html< /strong> 将 标签放入

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",

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-12-25 08:10:27

为什么不将按钮更改为:

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

然后在 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");
     }
  }
?>

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
  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");
     }
  }
?>
通知家属抬走 2024-12-25 08:10:27

制作一个像 一样简单的

-编辑-

哎呀,抱歉,看错了。

What about making one <button> that simply acts like a <a href=""> ?

-edit-

Whoops, sorry, misread. <button onclick="window.location='/nextpage';"> ?

笔芯 2024-12-25 08:10:27

实际上,我会在这里使用 JavaScript 和 onclick 事件,该事件会将您带到结账页面。

I would actually use JavaScript here with an onclick event, which will take you to the checkout page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文