繁体   English   中英

带有两个按钮的 Laravel 表单在提交时获取单击按钮的值

[英]Laravel form with two buttons get value of clicked button on submit

我有一个带有两个按钮的视图,当单击两个按钮之一时,我想接收该按钮的值。

<form action="{{ url('selectMode') }}" method="post">
    {{csrf_field()}}
    <button class="card mt-3"><i class="fas fa-laptop my-pr"></i>Computer<label>
            <input hidden name="mode" value="1">
        </label></button>
    <br>
    <button class="card-disabled mt-3"><i class="fas fa-user my-pr"></i>Person<label>
            <input hidden name="mode" value="2">
        </label></button>
</form>

这是我到目前为止的代码。 在控制器中转储请求时,该值始终为“2”。 我应该怎么做才能使这项工作?

刀片文件:

<form action="{{ route('store') }}" method="POST">
    <button type="submit" name="status" value="approve">Approve</button>
    <button type="submit" name="status" value="decline">Decline</button>
    <button type="submit" name="status" value="reject">Reject</button>
</form>

控制器:

public function store(Request $request)
{
    switch ($request->input('status')) {
        case 'approve':
            // Redirect to approve page
            break;

        case 'decline':
            // Redirect to decline page
            break;

        case 'reject':
            // Redirect to reject page
            break;
    }
}

你可以给一个按钮一个值

<button type="input" value="value">

或输入类型按钮

<input type="button" value="value">

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/button

暂无
暂无

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

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