1
votes

I have a form with a post method to update password of a user in the database but my problem is that the action in my form won't go to the the controller but the URL changes to what is written in the route. Here is my the code in my form.

<form method="POST" action="{{ route('Agent.ChangePass') }}">
<input type="hidden" value="{{ csrf_token() }}">
    <div class="col-sm-12 col-md-12">
        <div class="col-sm-12 col-md-12">
            <div class="form-group form-group-sm">
                <label>Old Password: </label>
                    <input type="password" class="form-control" name="old_pass" required>
            </div>
        </div>
        <div class="col-sm-12 col-md-12">
            <div class="form-group form-group-sm">
                <label>New Password: </label>
                <input type="password" class="form-control" name="new_pass" required>
            </div>
        </div>
        <div class="col-sm-12 col-md-12">
            <div class="form-group form-group-sm">
                <label>Re-enter New Password: </label>
                <input type="password" class="form-control" name="confirm_pass" required>
            </div>
                <input type="submit" class="btn btn-info btn-sm" value="Save"/>

        </div>
</div>
</form>

This is my route

Route::post('Agent/ChangePass', 'AgentsController@changePass')->name('Agent.ChangePass');

And this is my function in my controller

public function changePass(Request $request){
    dd($request);
    if (!(Hash::check($request->get('old_pass'), Auth::user()->password))) {
        // The passwords matches
        return redirect()->back()->with("error","Your current password does not matches with the password you provided. Please try again.");
    }

    if(strcmp($request->get('confirm_pass'), $request->get('new_pass')) != 0){
        //Current password and new password are same
        return redirect()->back()->with("error","Your new password does not match your re-entered password.");
    }

    //Change Password
    $user = Auth::user()->id;
    $users = Agent::find($users);
    $users->password = bcrypt($request->get('new_pass'));
    $users->save();

    return redirect()->back()->with("success","Password changed successfully !");
}
3
You may wish to rename your username to LaravelBeginner, so it is spelled correctly :-). - halfer
Just a note for any future questions, but please try to include any errors when posting a question. Depending on your .env settings, you would have seen either "Whoops, something went wrong." or a "TokenMismatchException"; both of which would have helped in determining the issue here. - Tim Lewis
Pro-tips: (1) you don't need to add home-made [LARAVEL] tags into titles here - there is a real tagging system, so just keep titles as sentences/questions in plain English. (2) If you can refrain from adding please-help-me or I-am-hoping-for-some-help that would be good as well - volunteer editors tend to trim this stuff anyway. You'll find that native English speakers sometimes understand this as a form of begging, so it is best avoided anyway. - halfer

3 Answers

0
votes

Try changing the route to:

Route::post('Agent/ChangePass', ['uses' => 'AgentsController@changePass'])->name('Agent.ChangePass');
0
votes

You could specify the url instead the route:

action="{{ url('Agent/ChangePass') }}"
0
votes

Thank you for your help, but I have already solved my problem. I just added name="_token" in my <input type="hidden" value="{{ csrf_token() }}">