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