I have checkbox, that who is checked. That's will be inserted in my table:inclusion.
I want to insert data, using checkbox with other textbox, but I don't have any idea for inserting data using checkbox, please help me out of this problem! Tyia!
Error: Invalid argument supplied for each()
Here's my code:
//script
$(document).on('submit', '#user_form', function(event){
event.preventDefault();
var roomname = $('#text_name').val();
var id = [];
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(roomname != '')
{
$.ajax({
url:"insert.php",
method:'POST',
data:new FormData(this),
// data: {inclusion_name:inclusion_name},
contentType:false,
processData:false,
success:function(data)
{
alert(data);
$('#user_form')[0].reset();
$('#userModal').modal('hide');
dataTable.ajax.reload();
}
});
}
else
{
alert("Both Fields are Required");
}
});
insert.php
$statement = $connection->prepare("
INSERT INTO rooms (roomname)
VALUES (:text_name)
");
$result = $statement->execute(
array(
':text_name' => $_POST["text_name"]
)
);
foreach($_POST["id"] as $id)
{
$statement = $connection->prepare("
INSERT INTO inclusion (inclusion)
VALUES (:id)
");
$result = $statement->execute(
array(
':id' => $_POST["id"]
)
);
}
TINYINT. It might be worth including a little snippet of the code for the checkbox so that people can help you better :)