简体   繁体   中英

My form is not submitted correctly in Laravel

I am trying to submit an user profile update form in my Laravel project. But while I click on the submit button that page just reload but not submit the form. also not given any error.

html code

 <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.

route

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();

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. 3.) CSRF hidden input pass like ( 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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