TomasAm's avatar

Submit button does not work after ajax request

I use ajax pagination for datasearch in my form.

When I open my page with the form, this form contains 5 rows which have checkboxes. I can mark one of them and click post. Data is saved into database. But I have also a search field, where you can look for further options. Once I use this search, the submit button does not respond anymore. What could be the issue?

My child view with pagination_data:

@foreach($data as $row)                                        
    <tr>
        <td>
            <div class="form-check">
            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="{{$row->id}}">
            <label class="form-check-label" for="exampleRadios1">
                        {{$row->id}}
            </label>
            </div>
            </td>
            <td>
            {{$row->title}}      
            </td>
        <td>empty</td>
    </tr>
@endforeach 

Form view in which I include child view pagination:

<div class="container">
   <h3 align="center">Laravel Live Data Search with Sorting & Pagination using Ajax</h3><br />
   <div class="row">
    <div class="col-md-9">

    </div>
    <div class="col-md-3">
     <div class="form-group">
      <input type="text" name="serach" id="serach" class="form-control" />
     </div>
    </div>
   </div>
   <div class="table-responsive">
    <table class="table table-striped table-bordered">
     <thead>
      <tr>
       <th width="5%" class="sorting" data-sorting_type="asc" data-column_name="id" style="cursor: pointer">ID <span id="id_icon"></span></th>
       <th width="38%" class="sorting" data-sorting_type="asc" data-column_name="post_title" style="cursor: pointer">Title <span id="post_title_icon"></span></th>
       <th width="57%">Description</th>
      </tr>
     </thead>
     <tbody>
<form method="post">
      @include('pagination_data')
<button type="submit" class="btn btn-primary">Submit</button>
</form>
     </tbody>
    </table>
    <input type="hidden" name="hidden_page" id="hidden_page" value="1" />
    <input type="hidden" name="hidden_column_name" id="hidden_column_name" value="id" />
    <input type="hidden" name="hidden_sort_type" id="hidden_sort_type" value="asc" />
   </div>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){

 function clear_icon()
 {
  $('#id_icon').html('');
  $('#post_title_icon').html('');
 }

 function fetch_data(page, sort_type, sort_by, query)
 {
  $.ajax({
   url:"/pagination/fetch_data?page="+page+"&sortby="+sort_by+"&sorttype="+sort_type+"&query="+query,
   success:function(data)
   {
    $('tbody').html('');
    $('tbody').html(data);
   }
  })
 }

 $(document).on('keyup', '#serach', function(){
  var query = $('#serach').val();
  var column_name = $('#hidden_column_name').val();
  var sort_type = $('#hidden_sort_type').val();
  var page = $('#hidden_page').val();
  fetch_data(page, sort_type, column_name, query);
 });

 $(document).on('click', '.sorting', function(){
  var column_name = $(this).data('column_name');
  var order_type = $(this).data('sorting_type');
  var reverse_order = '';
  if(order_type == 'asc')
  {
   $(this).data('sorting_type', 'desc');
   reverse_order = 'desc';
   clear_icon();
   $('#'+column_name+'_icon').html('<span class="glyphicon glyphicon-triangle-bottom"></span>');
  }
  if(order_type == 'desc')
  {
   $(this).data('sorting_type', 'asc');
   reverse_order = 'asc';
   clear_icon
   $('#'+column_name+'_icon').html('<span class="glyphicon glyphicon-triangle-top"></span>');
  }
  $('#hidden_column_name').val(column_name);
  $('#hidden_sort_type').val(reverse_order);
  var page = $('#hidden_page').val();
  var query = $('#serach').val();
  fetch_data(page, reverse_order, column_name, query);
 });

 $(document).on('click', '.pagination a', function(event){
  event.preventDefault();
  var page = $(this).attr('href').split('page=')[1];
  $('#hidden_page').val(page);
  var column_name = $('#hidden_column_name').val();
  var sort_type = $('#hidden_sort_type').val();

  var query = $('#serach').val();

  $('li').removeClass('active');
        $(this).parent().addClass('active');
  fetch_data(page, sort_type, column_name, query);
 });

});
</script>

Then my ajax controller

class PaginationController extends Controller
{
    function index()
    {
     $data = DB::table('post')->orderBy('id', 'asc')->paginate(5);
     return view('pagination', compact('data'));
    }

    function fetch_data(Request $request)
    {
     if($request->ajax())
     {
      $sort_by = $request->get('sortby');
      $sort_type = $request->get('sorttype');
            $query = $request->get('query');
            $query = str_replace(" ", "%", $query);
      $data = DB::table('post')
                    ->where('id', 'like', '%'.$query.'%')
                    ->orWhere('post_title', 'like', '%'.$query.'%')
                    ->orWhere('post_description', 'like', '%'.$query.'%')
                    ->orderBy($sort_by, $sort_type)
                    ->paginate(5);
      return view('pagination_data', compact('data'))->render();
     }
    }
}

Finally routes:



Route::get('/pagination', 'PaginationController@index');

Route::get('/pagination/fetch_data', 'PaginationController@fetch_data');

Route::post('/pagination, 'PaginationController@store');



0 likes
4 replies
Tangente's avatar
Tangente
Best Answer
Level 9

You are replacing the entire html inside tbody, remember, that also contains Form html tag.

So you are replacing this:

<tbody>
<form method="post">
      @include('pagination_data')
<button type="submit" class="btn btn-primary">Submit</button>
</form>

with this:

<tr>
        .....stuff here
    </tr>

and therefore

<form method="post">

is nowhere to find

I would do this if I were you:

<form method="post">
<div id="replaceThis">
      @include('pagination_data')
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

then in fetchdata function, do something like this:

function fetch_data(page, sort_type, sort_by, query)
 {
  $.ajax({
   url:"/pagination/fetch_data?page="+page+"&sortby="+sort_by+"&sorttype="+sort_type+"&query="+query,
   success:function(data)
   {

    $('#replaceThis').html(data);
   }
  })
 }
TomasAm's avatar

I appreciate the answer it gave me the idea. The submit form works now, but it does return the selected data as expected:

array:7 [▼
  "_token" => "SKGD6mvgCFGeHfzhbR2UL8DEYuL3KQFqeRKfo805"
  "title" => "test"
  "description" => "testas"
  "starts_at" => "2020-04-12"
  "ends_at" => "2020-05-01"
  "hours" => "54"
  "status" => "created"
]

It should return one more line from:

    <div class="form-check">
            <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="{{$row->id}}">
            <label class="form-check-label" for="exampleRadios1">

It works when I write code directly to form, but not when I include it from child.

Expected:

array:8 [▼
  "exampleRadios" => "test"
  "_token" => "SKGD6mvgCFGeHfzhbR2UL8DEYuL3KQFqeRKfo805"
  "title" => "test"
  "description" => "testy"
  "starts_at" => "2020-04-16"
  "ends_at" => "2020-04-25"
  "hours" => "54"
  "status" => "created"
]
Tangente's avatar

I don't know why you need to have that in the query results, you can have that in the html page.

But if you must have that format, I suggest you visit this page and see how you can make query in your controller:

https://laravel.com/docs/7.x/queries

TomasAm's avatar

Basically what I need simply is:

form, -someinput, -someinput, -input from ajax child view - this is not recongized by post method, since it comes from ajax, -someinput, submit, form.

it worked after I put the tag in the beginning of the html code instead:

<form method="post"> HERE
<div class="row">
    <div class="col-md-9">

    </div>
    <div class="col-md-3">
     <div class="form-group">
      <input type="text" name="serach" id="serach" class="form-control" />
     </div>
    </div>
   </div>
   <div class="table-responsive">
    <table class="table table-striped table-bordered">
     <thead>
      <tr>
       <th width="5%" class="sorting" data-sorting_type="asc" data-column_name="id" style="cursor: pointer">ID <span id="id_icon"></span></th>
       <th width="38%" class="sorting" data-sorting_type="asc" data-column_name="post_title" style="cursor: pointer">Title <span id="post_title_icon"></span></th>
       <th width="57%">Description</th>
      </tr>
     </thead>
     <tbody>
<form method="post"> INSTEAD HERE

It is solved, but If any idea why it happened let me know.

Please or to participate in this conversation.