0

I'm having a weird little problem with my controller. I don't want it to reload my page. I don't know if this is possible but it is mandatory that my page doesn't reload when calling this function. Doesn't matter if it gives a useless message as long as it doesn't reload. For the moment it returns back() as a placeholder. This is my controller:

public function update(Request $request, User $user)
    {
        $this->validate($request,[
            'name' => 'required|min:3|unique:users,name,' . $user->id,
            'email' => 'required|min:3|unique:users,email,' . $user->id
        ]);


        $user->name = $request->name;
        $user->email=$request->email;
        $user->active=$request->active;
        $user->admin=$request->admin;
        $user->save();

        session()->put('flash_message', 'User succesfully edited!');


//        return redirect('admin/users2')
//        sleep(10);
        return back();


    }

My form:

<form action="" method="post">
                    @method('put')
                    @csrf
                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" name="name" id="name"
                               class="form-control @error('name') is-invalid @enderror"
                               placeholder="name"
                               minlength="3"
                               required
                               value="">
                        @error('name')
                        <div class="invalid-feedback">{{ $message }}</div>
                        @enderror

                        <br>
                        <label for="email">email</label>
                        <input type="text" name="email" id="email"
                               class="form-control @error('email') is-invalid @enderror"
                               placeholder="email"
                               minlength="8"
                               required
                               value="">

                        @error('email')
                        <div class="invalid-feedback">{{ $message }}</div>
                        @enderror

                        {{--type hidden voor de waarde in te stellen als checkbox unchecked is--}}
                        <br>
                        <input type="hidden" name="active" value="0" />
                        <input type="checkbox" id="active" name="active" value=1> Active
                        <br>
                        <br>
                        <input type="hidden" name="admin" value="0" />
                        <input type="checkbox" id="admin" name="admin" value=1> Admin

                        <div class="invalid-feedback"></div>
                    </div>
                    <button style="display: inline" type="submit" class="btn saveadvanced btn-success">Save changes</button>
                </form>

I don't know if this is possible. If anyone knows a way to make this happen feel free to comment.

  • 3
    Sounds like you might need Ajax? But the problem is unclear. What, exactly, is the required process from the user's perspective? – Greg Schmidt Dec 29 '19 at 19:11
  • I'm having a table of users that has to be updated automatically without reloading the page. I can get that done but the only problem is that the page reloads and my professor doesn't want that. – stef vleugels Dec 29 '19 at 19:19
  • 1
    What does your form look like? – jeremykenedy Dec 29 '19 at 20:28
  • @jeremykenedy I updated the post with the form. – stef vleugels Dec 29 '19 at 20:47
  • Prevent the default form submission, then make the post request. Read up [here](https://www.w3schools.com/jquery/event_preventdefault.asp) and [here](https://github.com/axios/axios), respectively. Also, check out [this answer](https://stackoverflow.com/questions/20352799/ajax-form-submit-with-preventdefault) – FullStackOfPancakes Dec 30 '19 at 00:47

1 Answers1

0

First of all controller not make any request, it handle http request. If you don't want to reload your browser tab, you must to make this request through a ajax request.

And for this feature,you might be look at axios

For example:

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
Md. Amirozzaman
  • 1,083
  • 10
  • 21