我的表单未在 Laravel 中正确提交
My form is not submitted correctly in Laravel
我正在尝试在我的 Laravel 项目中提交用户配置文件更新表单。但是当我点击提交按钮时,该页面只是重新加载而不是提交表单。也没有给出任何错误。
html代码
<form action="{{ url('/profile-update') }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
<div class="form-group">
<label>Image</label>
<input id="edit_form_image" type="file" class="form-control" name="edit_form_image">
</div><!-- end form-group -->
<div class="form-group">
<label>Name <span class="form_mendatory">*</span></label>
<input id="edit_form_name" type="text" class="form-control" name="edit_form_name" value="{{Auth::user()->name}}">
</div><!-- end form-group -->
<div class="form-group">
<label>Spouse Name</label>
<input id="edit_form_spouseName" name="edit_form_spouseName" type="text" class="form-control" value="">
</div><!-- end form-group -->
<div class="form-group">
<label>Email <span class="form_mendatory">*</span></label>
<input id="edit_form_email" type="email" name="edit_form_email" class="form-control" value="{{Auth::user()->email}}">
</div><!-- end form-group -->
<div class="form-group">
<label>Contact No <span class="form_mendatory">*</span></label>
<input id="edit_form_contact_no" type="text" name="edit_form_contact_no" class="form-control" value="{{Auth::user()->contact_no}}">
</div><!-- end form-group -->
<div class="form-group">
<label>National ID</label>
<input id="edit_form_NID" type="text" name="edit_form_NID" class="form-control" value="">
</div><!-- end form-group -->
<div class="form-group">
<label>Profession <span class="form_mendatory">*</span></label>
<input id="edit_form_profession" type="text" name="edit_form_profession" class="form-control" value="{{Auth::user()->profession}}">
</div><!-- end form-group -->
<div class="form-group">
<label>Address</label>
<input id="edit_form_address" type="text" name="edit_form_address" class="form-control" value="">
</div><!-- end form-group -->
<input type="submit" class="btn btn-orange" value="Save Changes">
</form>
如果我将输入类型从 submit
更改为 button
,则不会重新加载或提交表单。
路线
Route::post('/profile-update','userController@profile_update');
userContoller.php
$request->validate([
'edit_form_image' => 'required|file|max:1024',
'edit_form_name' => 'required',
'edit_form_profession' => 'required',
'edit_form_email' => 'required',
'edit_form_contact_no' => 'required',
]);
$request->edit_form_image->store('logos');
$user = Auth::user();
$user->name = $request->edit_form_name;
$user->avatar = $request->edit_form_image;
$user->email = $request->edit_form_email;
$user->contact_no = $request->edit_form_contact_no;
$user->profession = $request->edit_form_profession;
$user->spouse_name = $request->edit_form_spouseName;
$user->edit_form_address = $request->edit_form_address;
$user->email = request('email');
$user->contact_no = request('contact_no');
$user->profession = request('profession');
$user->save();
问题出在哪里?
有人可以帮忙吗?提前致谢
有时 post 方法在更新时不工作,最好使用补丁方法 update.You 需要传递用户 ID,因为您要更新哪个用户 ID。
HTML
<form action="{{ route('updateProfile',$user->id)') }}" method="patch" enctype="multipart/form-data">
@csrf
路线
Route::post('/profile-update/{id}','userController@profile_update')->name('updateProfile');
希望对你有所帮助
乍一看,
1.) 就像验证失败错误
2.) 你能从 Route::post 中删除“/”并形成动作吗?
3.) CSRF 隐藏输入传递像 (https://laravel.com/docs/5.5/csrf).
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
如果以上解决方案不起作用,您能否确认请求将发送到服务器或控制台上是否有任何错误。
我正在尝试在我的 Laravel 项目中提交用户配置文件更新表单。但是当我点击提交按钮时,该页面只是重新加载而不是提交表单。也没有给出任何错误。
html代码
<form action="{{ url('/profile-update') }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
<div class="form-group">
<label>Image</label>
<input id="edit_form_image" type="file" class="form-control" name="edit_form_image">
</div><!-- end form-group -->
<div class="form-group">
<label>Name <span class="form_mendatory">*</span></label>
<input id="edit_form_name" type="text" class="form-control" name="edit_form_name" value="{{Auth::user()->name}}">
</div><!-- end form-group -->
<div class="form-group">
<label>Spouse Name</label>
<input id="edit_form_spouseName" name="edit_form_spouseName" type="text" class="form-control" value="">
</div><!-- end form-group -->
<div class="form-group">
<label>Email <span class="form_mendatory">*</span></label>
<input id="edit_form_email" type="email" name="edit_form_email" class="form-control" value="{{Auth::user()->email}}">
</div><!-- end form-group -->
<div class="form-group">
<label>Contact No <span class="form_mendatory">*</span></label>
<input id="edit_form_contact_no" type="text" name="edit_form_contact_no" class="form-control" value="{{Auth::user()->contact_no}}">
</div><!-- end form-group -->
<div class="form-group">
<label>National ID</label>
<input id="edit_form_NID" type="text" name="edit_form_NID" class="form-control" value="">
</div><!-- end form-group -->
<div class="form-group">
<label>Profession <span class="form_mendatory">*</span></label>
<input id="edit_form_profession" type="text" name="edit_form_profession" class="form-control" value="{{Auth::user()->profession}}">
</div><!-- end form-group -->
<div class="form-group">
<label>Address</label>
<input id="edit_form_address" type="text" name="edit_form_address" class="form-control" value="">
</div><!-- end form-group -->
<input type="submit" class="btn btn-orange" value="Save Changes">
</form>
如果我将输入类型从 submit
更改为 button
,则不会重新加载或提交表单。
路线
Route::post('/profile-update','userController@profile_update');
userContoller.php
$request->validate([
'edit_form_image' => 'required|file|max:1024',
'edit_form_name' => 'required',
'edit_form_profession' => 'required',
'edit_form_email' => 'required',
'edit_form_contact_no' => 'required',
]);
$request->edit_form_image->store('logos');
$user = Auth::user();
$user->name = $request->edit_form_name;
$user->avatar = $request->edit_form_image;
$user->email = $request->edit_form_email;
$user->contact_no = $request->edit_form_contact_no;
$user->profession = $request->edit_form_profession;
$user->spouse_name = $request->edit_form_spouseName;
$user->edit_form_address = $request->edit_form_address;
$user->email = request('email');
$user->contact_no = request('contact_no');
$user->profession = request('profession');
$user->save();
问题出在哪里?
有人可以帮忙吗?提前致谢
有时 post 方法在更新时不工作,最好使用补丁方法 update.You 需要传递用户 ID,因为您要更新哪个用户 ID。
HTML
<form action="{{ route('updateProfile',$user->id)') }}" method="patch" enctype="multipart/form-data">
@csrf
路线
Route::post('/profile-update/{id}','userController@profile_update')->name('updateProfile');
希望对你有所帮助
乍一看, 1.) 就像验证失败错误 2.) 你能从 Route::post 中删除“/”并形成动作吗? 3.) CSRF 隐藏输入传递像 (https://laravel.com/docs/5.5/csrf).
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
如果以上解决方案不起作用,您能否确认请求将发送到服务器或控制台上是否有任何错误。