title: Laravel & Ajax - Insert data into table without refreshing
body: <p>First of all, I have to say that I'm beginner with using Ajax... So help me guys.
  I want to insert the data into db without refreshing the page. So far, I have following code...
  In blade I have a form with an id:</p>
  
  <pre><code>{!! Form::open(['url' =&gt; 'addFavorites', 'id' =&gt; 'ajax']) !!}
  
    &lt;a href="#" id="favorite" class="bookmark"&gt;&lt;img align="right" src="{{ asset('/img/icon_add_fav.png')}}"&gt;&lt;/a&gt;
    &lt;input type="hidden" name = "idUser" id="idUser" value="{{Auth::user()-&gt;id}}"&gt;
    &lt;input type="hidden" name = "idArticle" id="idArticle" value="{{$docinfo['attrs']['sid']}}"&gt;
    &lt;input type="submit" id="test" value="Ok"&gt;
  
  {!! Form::close() !!}
  </code></pre>
  
  <p>And in controller I have:</p>
  
  <pre><code>public function addFavorites()
  {
      $idUser               = Input::get('idUser');
      $idArticle            = Input::get('idArticle');
      $favorite             = new Favorite;
      $favorite-&gt;idUser     = $idUser;
      $favorite-&gt;idArticle  = $idArticle;
      $favorite-&gt;save();
  
      if ($favorite) {
          return response()-&gt;json([
              'status'     =&gt; 'success',
              'idUser'     =&gt; $idUser,
              'idArticle'  =&gt; $idArticle]);
      } else {
          return response()-&gt;json([
              'status' =&gt; 'error']);
      }
  }
  </code></pre>
  
  <p>I'm trying with ajax to insert into database:</p>
  
  <pre><code>   $('#ajax').submit(function(event){
        event.preventDefault();
  
     $.ajax({
        type:"post",
        url:"{{ url('addFavorites') }}",
        dataType="json",
        data:$('#ajax').serialize(),
        success: function(data){
           alert("Data Save: " + data);
        }
        error: function(data){
           alert("Error")
        }
     });
     });
  </code></pre>
  
  <p>Also in my <code>web.php</code> I have a route for adding favorites. But when I submit the form, it returns me JSON response like this: <code>{"status":"success","idUser":"15","idArticle":"343970"}</code>... It actually inserts into the db, but I want the page not to reload. Just to display alert box.</p>