简体   繁体   中英

How to check and uncheck checkbox automatically

I'm doing a page where i can display a list of data with checkboxes. then i will automatically check with database and if the data is there, the checkbox will be checked. What i want to do now is, lets say the checkbox is checked automatically, andwhen i unchecked it myself and save, it will update the data in database.

Example of my coding:

<form action="{{URL::to('/granted/'.$d->id)}}" method="get">
    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
    @foreach($var as $v)
    <table style="margin-left:20px;">
         <caption>{{$v->module_groupname}}</caption>
         @foreach($name as $n)
         <?php if($n->module_groupname == $v->module_groupname){?>
         <tr><td width="80"><input type="checkbox" name="module_code[]" value="{{ $n->module_code }}"
         <?php foreach($priv as $p){
         if($p->user_id == $d->id){
             if($p->module_code == $n->module_code){
                 echo 'checked="checked"';
             }
         }
     }?>></td>
     <td width="150">{{$n->module_groupname}}</td>
     <td width="200">{{ $n->module_desc}}</td>
    </tr> <?php } ?>
 @endforeach
</table>
<hr>
@endforeach
<input type="submit" value="select" class="btn btn-s btn-success">
</form>

i've been advised to lookup for PLUS statement oracle but i couldnt find how

for automatically checked you need set Attribute checked for that checkbox

https://www.w3schools.com/tags/att_input_checked.asp

for doing this by js you follow this url ! say setAttribute property

https://www.w3schools.com/jsref/met_element_setattribute.asp

hope this will help you

<input type="checkbox" @if(compare) checked @endif />

I recommend you to look at this code and try to reformat you own code. You are using blade so use it everywhere.

<form action="{{URL::to('/granted/'.$d->id)}}" method="get">
{{ csrf_field() }}
@foreach($var as $v)
    <table style="margin-left:20px;">
        <caption>{{$v->module_groupname}}</caption>
        @foreach($name as $n)
            @if($n->module_groupname == $v->module_groupname)
                <tr>
                    <td width="80"><input type="checkbox" name="module_code[]" value="{{ $n->module_code }}" {{ $priv->contains('user_id', $d->id) && $priv->contains('module_code', $n->module_code) ? 'checked' : '' }}></td>
                    <td width="150">{{$n->module_groupname}}</td>
                    <td width="200">{{ $n->module_desc}}</td>
                </tr>
            @endif
        @endforeach
    </table>
    <hr>
@endforeach
<input type="submit" value="select" class="btn btn-s btn-success">

I think you can use the contains function if your $prev variable is a model.

I didn't test this code so try if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM