In Laravel 5.1 Can't post value of checkbox

允我心安 提交于 2020-01-15 20:58:12

问题


I have 2 checkboxs inside a form, I can't post value of them. I have already tried this:

<div class="form-group col-md-4">
                <label class="col-md-6" for="invoiced">
                    <input type="checkbox" id="invoiced" value="false" name="project[invoiced]"> 
                     Invoiced </label>
                <label class="col-md-6" for="tobeinvoiced">
                    <input type="checkbox" id="tobeinvoiced" value="true" name="project[tobeinvoiced]" checked> 
                    To be Invoiced </label>
            </div>

with this script that changes the value of 2 checkboxes in true or false:

<script type="text/javascript">
            $('#tobeinvoiced').change(function(){
                cb = $(this);
                cb.val(cb.prop('checked'));
            });
            $('#invoiced').change(function(){
                cb = $(this);
                cb.val(cb.prop('checked'));
            });
            </script>

but when I submit, the values passed are set to null.


回答1:


Try this

$project = $request->input('project');
$tobeinvoiced = $project['tobeinvoiced'];

Basically the naming convention you are using makes it an array and you cant call an array directly in request input in laravel

Hope this helps.




回答2:


I've found the solution to my problem, this is the link : Solution.

My code became:

<label class="col-md-6" for="invoiced">
   <input type="checkbox" key="invoiced"/>
   <input type="hidden" id="invoiced" value="0" name="project[invoiced]"> 
   Invoiced </label>
<label class="col-md-6" for="tobeinvoiced">
   <input type="checkbox" key="tobeinvoiced" checked/>
   <input type="hidden" id="tobeinvoiced" value="1" name="project[tobeinvoiced]"> 
   To be Invoiced </label>

with this script:

 $(document).ready(function () {
                $('[key]').change(function () {
                    var key = $(this).attr('key');
                    $($('[name="project[' + key + ']"]')).val($(this).is(':checked') ? 'true' : 'false');
                });
            });


来源:https://stackoverflow.com/questions/33466411/in-laravel-5-1-cant-post-value-of-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!