Suganya Rajasekar
Suganya Rajasekar

Reputation: 684

how to pass checkbox value from view to controller in laravel while clicking checkbox

Here is my code: View

    <label>
<input class="i-check" name="amenity[]" type="checkbox" value="{{$amn->ht_amenity_id}}" />{{$amn->ht_amenity_name}}
     </label>

Controller

$purp = $_POST['amenity'];
    print_r($purp);

Here (In controller), I'm just trying to print the value

Please Some one help me.

Upvotes: 1

Views: 1647

Answers (2)

Arya Basiri
Arya Basiri

Reputation: 177

Use AJAX

in View:

$(selector).change(function()
{
    if($(selector).is(':checked'))
        checked = 'checked';
    else
        checked = 'unchecked';
    $.post('The page u want', checked:checked, function(data){});
});

in Controller:

The code that you mentioned:

$purp = $_POST['checked'];
    print_r($purp);

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

You use Laravel, so you should do it the right way by using Laravel Collective with it's Form::model and Form::open constructions:

https://laravel.com/docs/4.2/html#checkboxes-and-radio-buttons

Laravel will pass the data to store() or update() method inside controller for you. Then you can use it with something like: $request->get('name')

Also, learn about RESTful controllers, it's the easiest way to work with forms: https://laravel.com/docs/5.1/controllers#restful-resource-controllers

Upvotes: 0

Related Questions