简体   繁体   English

我的表格未在Laravel中正确提交

[英]My form is not submitted correctly in Laravel

I am trying to submit an user profile update form in my Laravel project. 我正在尝试在Laravel项目中提交用户个人资料更新表格。 But while I click on the submit button that page just reload but not submit the form. 但是,当我单击“提交”按钮时,该页面只是重新加载而不提交表单。 also not given any error. 也没有给出任何错误。

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

If I change the input type from submit to button then no reload or submit of the form. 如果我将输入类型从“ submit更改为“ button则不会重新加载或提交表单。

route 路线

Route::post('/profile-update','userController@profile_update');

userContoller.php 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();

Where is the problem? 问题出在哪儿?

Anybody can help ? 有人可以帮忙吗? Thanks in advance 提前致谢

From first look, 1.) it's like validation failing error 2.) can you remove "/" from the Route::post and form actions. 乍看之下,1。)就像验证失败错误2.)您可以从Route :: post和form操作中删除“ /”吗? 3.) CSRF hidden input pass like ( https://laravel.com/docs/5.5/csrf ). 3.)CSRF隐藏输入通道,例如( https://laravel.com/docs/5.5/csrf )。

<form method="POST" action="/profile">
    {{ csrf_field() }}
    ...
</form>

If above solutions not work then, can you confirm that the request will sent to the server or any error on console. 如果上述解决方案不起作用,您是否可以确认请求将发送到服务器或控制台上是否有任何错误。

Sometimes post method not woking while update better use patch method to update.You need to pass user id because which user id you want to update. 有时更新后发布方法不起作用,最好使用补丁方法进行更新。您需要传递用户ID,因为您要更新哪个用户ID。

HTML HTML

<form action="{{ route('updateProfile',$user->id)') }}" method="patch" enctype="multipart/form-data">
@csrf

route 路线

Route::post('/profile-update/{id}','userController@profile_update')->name('updateProfile');

I hope this i may help you. 我希望这可以为您提供帮助。

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

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