如何检查PHP中是否未选中复选框?

时间:2018-08-09 17:52:05

标签: javascript php jquery

我在表中有一些MySQL数据。每个数据都有一个编辑按钮。如果单击其中一个按钮,则会显示具有该行属性的引导程序模式。我在模态中有一个复选框。如果单击它,将显示一个隐藏的文本输入。如果我再次单击它,它就会消失。如果隐藏的文字输入不为空,则在出现模态时会显示出来。

还有我的问题:如果隐藏文本输入不为空,并且我未选中该复选框,那么我想从数据库中删除数据。我花了很多时间来解决这个问题,但是id没用。我怎样才能做到这一点?现在,如果我打开模态,它将删除数据。

index.php

<!-- Modal -->
<div id="companyModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <form method="post" id="company_form">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <div class="col-md-10">
                        <b>Licensees: </b><input type="checkbox" name="licensee" id="licensee"/>
                    </div>
                    <div id="hidden" class="form-group">
                        <hr/>
                        <input type="text" name="name" id="name">
                        <hr/>
                    </div>
                    <input class="action" type="hidden" name="company_id" id="company_id" />
                    <input class="action" type="hidden" name="btn_action" id="btn_action" />
                    <input type="submit" name="action" id="action" class="btn btn-info action" value="Add" />
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
                </div>
            </div>
        </form>
    </div>
</div>

<script>
   $(document).on('click', '.update', function() {
            var company_id = $(this).attr("id");
            $.ajax({
                url:"company.php",
                method:"POST",
                data:{company_id:company_id},
                dataType:"json",
                success:function(data) {
                    $('#companyModal').modal('show');
                    $('#name').val(data.name);
                    $('#company_id').val(company_id);

                    var name = jQuery.trim($("#type").val());

                    $('[name=licensee]').click(function(){
                        if ($(this).is(":checked")) {
                            $("#licensee").val("checked");
                            $("#hidden").show();
                            $("#name").attr("required", true);
                        } else {
                            if(name.length > 0){
                                if (confirm("Are you want to delete this?")) {
                                    alert("Delete");
                                    $("#hidden").hide();
                                    $("#licensee").prop("checked", false);
                                    $("#name").attr("required", false);
                                    $("input[name^='name']").val("");
                                } else {
                                    alert("Canceled");
                                    $("#hidden").show();
                                    $("#licensee").prop("checked", true);
                                    $("#name").attr("required", true);
                                }
                            } else {
                                $("#hidden").hide();
                                $("#licensee").prop("checked", false);
                                $("#name").attr("required", false);    
                            } 
                        }
                    });

                    if(name.length > 0) {
                        $("#licensee").prop("checked", true);
                        $("#hidden").show();
                    } else if(name.length === 0){
                        $("#hidden").hide();
                        $("#licensee").prop("checked", false);
                    }
                }
            });
        });
</script>

company.php

// It deletes the datas when the modal appears
if(!isset($_POST['licensee'])){
    $delete_query = "DELETE company_licensee, licensee
            FROM company_licensee
            LEFT JOIN licensee ON company_licensee.licensee_id = licensee.licensee_id
            WHERE company_licensee.company_id = ?";
        $statement = $connect -> prepare($delete_query);
        $statement -> bindParam(1, $_POST["company_id"], PDO::PARAM_INT);
        $statement -> execute();
}

0 个答案:

没有答案