Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 1 year ago.
0

I figured I should post the Blade view too:

@foreach ($user->topics as $topic)
          
            <tr>

              <td>{{{ $topic->title }}}</td>

              <td>

                <form role="form" class="formNotify" action="{{ URL::route('topic-notify-post') }}" method="post">

                  <input type="hidden" name="_token" value="{{{ Session::getToken() }}}">
                  <input type="hidden" name="topic_id" value="{{{ $topic->id }}}">
                  <input type="hidden" name="notify" value="{{{ $topic->pivot->notify }}}">

                  @if ($topic->pivot->notify == 1)
                    
                    <button id="toggle{{ $topic->id }}" class="btn btn-primary" type="submit">On</button>
                  
                  @else

                    <button id="toggle{{ $topic->id }}" class="btn btn-default" type="submit">Off</button>
                  
                  @endif

                </form>

              </td>

            </tr>
          
          @endforeach
Last updated 1 year ago.
0

It would be good to see the controller and the view you are using with jQuery.

My guess from looking at the jQuery is that you are always using 'POST'. An update should use 'PUT'. Because browsers don't support PUT laravel implements a standard of using a '_method' form element which will be set to PUT or DELETE if trying to use either of those methods. Laravel will treat those requests as authentic PUT or DELETE requests.

Try this

musixite said:

   jQuery( document ).ready( function( $ ) {
  
      $( '.formNotify' ).on( 'submit', function() {

       var $form = $( this ),
             action = $form.find('input[name=_method]').val() || $form.prop('action');
       $.post(
           action,

           {
               "_token": $( this ).find( 'input[name=_token]' ).val(),
               "topic_id": $( this ).find( 'input[name=topic_id]' ).val(),
               "notify": $( this ).find( 'input[name=notify]' ).val()
           },

           ...

This might fix it

Last updated 1 year ago.
0

Hi Damien,

Thanks for your answer. Unfortunately, it didn't work

Here's my controller:

public function postNotify() {

        //check if its our form
        if ( Session::token() !== Input::get( '_token' ) ) {
            return Response::json( array(
                'msg' => 'Unauthorized attempt to create setting'
            ) );
        }

        $topic_id = Input::get('topic_id');
        $user = Auth::user();

        if (Input::get('notify') == 1) $notify = 0;
        if (Input::get('notify') == 0) $notify = 1;

        Topic::find($topic_id)->users()->updateExistingPivot($user->id, array('notify' => $notify));

        $response = array(
            'status' => 'success',
            'msg' => 'Setting created successfully',
            'topic_id' => $topic_id,
            'notify'   => $notify
        );
     
        return Response::json( $response );

    }

The view is above and yes I'm using a POST form (I just followed a tutorial assuming this was the right way).

From what I've read, it seems it has more to do with the DOM. The first time the form is submitted, I remove a class on the submit button which apparently removes it from the DOM meaning it's not a submit button anymore.

My explanation is very vague, so is my understanding but maybe it will give you a clue :)

Thanks again,

  • V
Last updated 1 year ago.
0

Is the POST request called when you submit form second time or not?

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

musixite musixite Joined 17 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.