[英]Getting value of a submit button in Laravel 5.3
我的頁面上有一個表單
<form method="post" action="{{url('/vpage')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="w100">
<button name="hostel1" class="submitBTN addnowBtn" type="submit" value="The Venetian"> Add Now</button>
</div><!--w100-->
</form>
我把請求打印在我的控制器中
public function vegaspage(Request $request){
dd($request);
die;
}
我的頁面上也有很多字段,當請求參數進入瀏覽器時,提交按鈕值沒有進入請求任何想法?
在你的控制器功能內試試這個:
Input::get('hostel1', 'NA');
// It will return its value ie `The Venetian` otherwise `NA`
注意: Input::get()
的第二個參數是默認值。
注意:最簡單的調試方法是通過Google Chrome中的“網絡”標簽。 您可以看到標題響應數據。
但這不起作用的原因可能是因為你正在做一個POST請求。 如果您執行GET請求,您將獲得該按鈕的值。
另一個原因可能是你正在通過javascript進行提交並執行e.preventDefault()
在這種情況下你並沒有真正發送請求。 所以PHP沒有得到價值。
做
$request->hostel1
當你想dd()
你的輸入參數時,做
dd($request->all());
將按鈕代碼更改為:
<input name="hostel1" value="The Venetian" type="hidden">
<button class="submitBTN addnowBtn" type="submit"> Add Now</button>
在控制器中,使用它來獲取值:
$request->hostel1
我意識到,只要我嘗試將名稱和值設置為“提交”按鈕,laravel就不會檢索請求中的值。 所以你可能想要使用這樣的隱藏字段:
<input type="hidden" name="hostel1" value="hostel1">
並在服務器端檢索它,如下所示:
$request->hostel1;
通過使用請求命名空間,您可以獲取任何輸入參數的值,也可以獲取提交按鈕的值。
為此,您必須在控制器中包含Request,例如:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//IN your problem you can submit button value as
$submit_button = $request->input('hostel1');
}
}
有關請求的更多詳細信息,您可以遵循:
https://laravel.com/docs/5.3/requests
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.