drkxgs9358 2018-08-09 17:52
浏览 58

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

I have some MySQL data in a table. Each data has an edit button. If I click one of the button, a bootstrap modal appears with the properties of that row. I have a checkbox in a modal. If I click on it, a hidden text input shown. If I click on it again, it's disappear. If the hidden text input didn't empty, it's shown when the modal appears.

And my problem: If the hidden text input not empty, and I unchecked the checkbox, I want to delete the datas from the database. I spent a lot of time to solve this, but id didn't work. How can I do this? Now it's delete the datas if I open the modal.

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘