1

I have written a code that sends email to selected people using check-box, now I have to add select all and clear all button so that all the check-boxes should get selected/dis-selected.

Here is my code:

My controller:

 public function display(){
        $data = Input::get('agree');
        $count = count ($data);
        $this->contact( $data );
        return view('clientinfo.display', compact('data','count'));
    }

My View:

@extends('app')
@section('content')

    <h1>Welcome! Send e-mail to selected people.</h1>
    <hr>
    {!! Form::open(array('action' => 'ClientsController@display','method' => 'GET'))!!}
        @foreach($clients as $client)
            {!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email]) !!}
                <article>
                    {{ $client->user_name }}
                    &nbsp;
                    {{ $client->email }}

                </article>
        @endforeach
        {!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}

    {!! Form::close() !!}
    <br/>
    @include('clientinfo.newmember')
@stop

if i add a button in my view naming select-all and clear-all how do i relate them, to select all the members within list?

2 Answers 2

0

I have made changes to my code and it worked using JavaScript.

Here is my code:

@extends('app')
@section('content')

    <h1>Welcome! Send e-mail to selected people.</h1>
    <hr>
    {!! Form::open(array('action' => 'ClientsController@display','method' => 'GET','name'=>'f1' , 'id'=>'form_id'))!!}
    <br/>
        @foreach($clients as $client)
            <div class="form-group">
            {!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email], ['class' => 'questionCheckBox']), $client->email !!}
            <br/>
            </div>
        @endforeach
    <div class="form-group">
    {!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}
    </div>

    <div class="form-group">
    {!! Form::button('Select All',['class' => 'btn btn-info form-control','onClick'=>'select_all("agree", "1")']) !!}
    </div>

    <div class="form-group">
    {!! Form::button('Clear All',['class' => 'btn btn-danger form-control','onClick'=>'select_all("agree", "0")']) !!}
    </div>

    {!! Form::close() !!}
@stop
@endsection

And in my master file,I have added a JavaScript:

    <script type="text/javascript">

    var formblock;
    var forminputs;

    function prepare() {
        formblock= document.getElementById('form_id');
        forminputs = formblock.getElementsByTagName('input');
    }

    function select_all(name, value) {
        for (i = 0; i < forminputs.length; i++) {
            // regex here to check name attribute
            var regex = new RegExp(name, "i");
            if (regex.test(forminputs[i].getAttribute('name'))) {
                if (value == '1') {
                    forminputs[i].checked = true;
                } else {
                    forminputs[i].checked = false;
                }
            }
        }
    }

    if (window.addEventListener) {
        window.addEventListener("load", prepare, false);
    } else if (window.attachEvent) {
        window.attachEvent("onload", prepare)
    } else if (document.getElementById) {
        window.onload = prepare;
    }
</script>

Earlier i was doing this using Jquery, that was not working, then i tried using JavaScript and it worked. I have tried various ways to add jquery in my code which are given in answers of stackoverflow.com, but nothing worked in laravel. If anyone know how to use jquery in laravel, please leave a comment.

Sign up to request clarification or add additional context in comments.

Comments

0

You could give the checkboxes a class and use jquery. You also need a checkbox that says "select all" with an ID.

$(function() {
    
    $('#selectAll').click(function() {
        if ($(this).prop('checked')) {
            $('.your_checkbox_class').prop('checked', true);
        } else {
            $('.your_checkbox_class').prop('checked', false);
        }
    });
    
});

The above code is checked by me. It works properly in new versions. I checked it with laraval 10

2 Comments

@Kiwi_Juicer : Can you tell me how to use jquery in laravel?
Sorry for the late answer. You should be able to just include the library according to this post: stackoverflow.com/questions/32498328/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.