0

I want to display a table using ajax when something is typed in the search box. Before generating any table I am just testing that the ajax call is reached to the controller or not by printing the searched keyword. But it is not working. I think the problem is in the url. But I don't know what could be the solution.

Here is my view:

 <input class="form-control" type="text" name="searched_key"    id="searched_key">

 <div id="live-data"></div>

Here is my ajax:

 <script>
  $(document).ready(function () {
    $("#searched_key").keyup(function () {
        var string = $(this).val();
        $.ajax({
          type: "post",
          url: "form_value",
          data: {searched_key: $string},
          success: function (data) {
              $("#live-data").html(data);
          }
      });

  });

});

Here is my controller:

class HomeController extends Controller {
 public function get_applicants_info(){

     $key_word = $_POST['searched_key'];

     echo $key_word;  //to test the ajax call
}
}

Here is my routes:

Route::get('/', 'HomeController@index');

Route::post('form','HomeController@store');

Route::post('form_value','HomeController@get_applicants_info');
3
  • I take it nothing is happening on the page? Go to developer tools -> network and click on the XHR tab. See what response is being sent back. Commented Jul 18, 2016 at 22:30
  • check networks tab Commented Jul 18, 2016 at 22:35
  • ^ check your networks tab and see what request is actually being sent and where it is going.
    – James
    Commented Jul 19, 2016 at 1:42

1 Answer 1

1

I think that the problem is with your AJAX url. Instead of specifying the URL directly try using url() function (In case you are using blade engine).

<script>
  $(document).ready(function () {
    $("#searched_key").keyup(function () {
        var string = $(this).val();
        $.ajax({
          type: "post",
          url: "{{url(form_value)}}", <= change your url to this.
          data: {searched_key: $string},
          success: function (data) {
              $("#live-data").html(data);
          }
      });

  });

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.