3

Here is my code, in "View" I wrote like this :

JS :

$(document).ready(function()
{
    $( ".iCheck-helper" ).on( "click", function(){
        console.log($('.i-check:checked').map(function() {
          //alert(this.value);
          return this.value;
        }).get().join(', '));
    }) ;
});

I should pass the returned value from this javascript to the controller file.

How can I do this please someone help me...

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Suganya Rajasekar
  • 685
  • 3
  • 14
  • 37

1 Answers1

2

You could use ajax request to send values you want :

$(document).ready(function()
{
  $( ".iCheck-helper" ).on( "click", function(){
      var value_to_send = $('.i-check:checked').map(function() {
          return this.value;
      }).get().join(', '));

      $.get('route_to_your_action', {value_name: value_to_send},function(data)           {
          //data contain response from controller action
          alert(data);
      })
  });
});

In the other side inside contoller you could get the value passe using Input :

use Illuminate\Support\Facades\Input;
.
.
.
function postHotelresults(){
    $value_name = Input::get('value_name');
}

Hope this helps

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • I would like use the "value_to_send" in my Controller function named as postHotelresults()... By using this $.get('route_to_your_action', {value_name: value_to_send},function(data) { }) How can I pass it – Suganya Rajasekar Feb 29 '16 at 05:07
  • Already passed in `{value_name: value_to_send}` you could get it in the controller by name `value_name` e.g : `Input::get('value_name')`. – Zakaria Acharki Feb 29 '16 at 09:12
  • I'm trying to print that value in controller but it didn't print any values.. What should I give instead of ""route_to_your_action"" my URL is ""hotel/hotelresults"" and my Controller name is ""HotelController"" inside this I need to access this value in the function named as ""postHotelresults"" – Suganya Rajasekar Feb 29 '16 at 09:59
  • You could not print it in controller, you could store it somewhere or use it for some operation, and you could return it to javascript callback and show it (check my updated answer). – Zakaria Acharki Feb 29 '16 at 10:00
  • What should I give instead of ""route_to_your_action"" Whether use the function name or url – Suganya Rajasekar Feb 29 '16 at 10:03
  • You should add new route for your action in route.php file as `Route::get('route_to_your_action', 'HotelController@postHotelresults');`. – Zakaria Acharki Feb 29 '16 at 10:10
  • You may also find you need a backslash before your route depending on how web.php is set up: `$.get('route_to_your_action', ` might need to be `$.get('/route_to_your_action', ` – AKMorris Oct 05 '17 at 04:39