I have 5 inputs and one html table. I need to check the checkbox that are in the html table so I need to loop through the table and get the data. The 5 inputs and the data on html table are related so if one of the insert statement fails, this will be a problem.
So far validating the inputs and checking the html table checkbox are working, but what if one of my insert statement failed? That being said, I need to delete the data inserted if one fails.
This is the structure of my website:
- Validate the inputs
- Loop through html table where checkbox is checked
- AJAX code here calling
insert
if
statement here to check if the checkbox is checkedif
yes then
-> AJAX code here
-> Call anotherinsert
statement
else
->no data to be saved, because the inputs and html table value need each other
UPDATE:
if im going to use transaction how can i apply it? on my website im using two different php file for insert. one in the loop. and one for the 5 inputs this is my code
var checkedItems = $('#dataTable input[type="checkbox"]:checked').each(function() {
$.ajax({
type: "POST",
url: "insertdocumentsignatory.php",
data: ({dtnum: tnum, dsignum: signum})
})
.done(function (msg) {
alert("Data Saved: " + msg);
})
.fail(function() {
alert( "Posting failed." );
});
});
});
if (!checkedItems.size()) {
alert ("Nothing checked");
return;
}else if (checkedItems.size()) {
$.ajax({
type: "POST",
url: "insertdocument.php",
data: ({dtnum: tnum, ddoctitle: doctitle, ddoctype: doctype, ddoccontent: doccontent, ddocdatefilled: docdatefilled})
})
.done(function (msg) {
alert("Data Saved: " + msg);
})
.fail(function() {
alert( "Posting failed." );
});
}