Home > Software design >  Insert selected checkbox value to database using laravel
Insert selected checkbox value to database using laravel

Time:03-02

I'm trying to insert selected checkbox value into my database but I don't seem to know how because I'm new to laravel.

This is the foreach code:

@foreach($users as $user)
                                    <?php $id  =1; ?>
                                    <ul >
                                        <div >
                                            <li >
                                                <div >
                                                    <input type="checkbox" value="{{ $user->name }}" onclick="checkBox(this)"  type="checkbox" id="approver"
                                                        value="{{ $user->name }}">
                                                    <div >
                                                        <img  src="{{ url('/assets/img/avatars/3.png') }}">
                                                    </div>
                                                    <div >
                                                        <div >{{ $user->name }}</div>
                                                        <label for="" >{{ $user->email }}</label>
                                                    </div>
                                                </div>
                                            </li>
                                        </div>
                                    </ul>
                                    @endforeach

Here is my checkbox code:

                        <div  style="background-color: #F2F2F2">
                            <div id="listUser"></div>
                                <ol  style="display:none">
                                    <div >
                                        <li >
                                            <div >
                                                <div >
                                                    <img  src="{{ url('/assets/img/avatars/3.png') }}">
                                                </div>
                                                <div >
                                                <div >{{ $users[0]->name }}</div>
                                                </div>
                                            </div>
                                        </li>
                                    </div>
                                </ol>
                            </div>

And my javascript code:

function checkBox(cb) {
        var inners = '<ol id="'  cb.value  '" >'  
            '<div >'  
            '<li >'  
            '<div >'  
            '<div >'  
            '<img  src="{{ url('/assets/img/avatars/3.png') }}">'  
            '</div>'  
            '<div >'  
            '<div >'   cb.value   '</div>'  
            '</div>'  
            '</li>'  
            '</div>'  
            '</ol>';
            console.log(cb.value);

        if (cb.checked == true) {
            //text.style.display = 'block';
            document.getElementById('listUser').innerHTML  = inners;
        } else {
            // text.style.display = 'none';
        document.getElementById(cb.value).remove();
        }
    }

The data I'm trying to insert is the name and email. Is there any solutions?

CodePudding user response:

I think you missed something

  1. You are forget to add name attribute in your checkbox (also you have duplicate value attribute) <input type="checkbox" value="{{ $user->name }}" onclick="checkBox(this)" type="checkbox" id="approver" value="{{ $user->name }}">

  2. Your email doesn't have any checkbox

  • Related