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">×</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();
}