<form id='add-ksa-form' action='#' method='post' border='0'>
<div class="modal-body">
<p>
<?php
echo "<table class='d_input'>";
echo "<tr>";
echo "<div id='success'></div>";
echo "</tr>";
echo "<tr>";
echo "<td>Region:</td>"."<td>";
require('config.php');
$stmt = $pdo->prepare("select name from regions where country='KSA' order by name");
$stmt->execute();
$data = $stmt->fetchAll();
echo "<select name='region' id='region'>";
 foreach ($data as $row): 
echo "<option>".$row['name']."</option>";
endforeach;
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Date:</td>"."<td>
<div class='input-group date'>
<input type='text' readonly='readonly' name='registration_date' id='registration_date' class='form-control' value=''>
<div class='input-group-append' id='input_btn_calendar'><span class='input-group-text'><i class='fa fa-calendar' aria-hidden='true'></i></span></div>
</div>
</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Total:</td>"."<td><input type='text' name='total' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Male:</td>"."<td><input type='text' name='male' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Female:</td>"."<td><input type='text' name='female' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Children:</td>"."<td><input type='text' name='minors' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Children Male:</td>"."<td><input type='text' name='children_male' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<tr>";
echo "<td>Children Female:</td>"."<td><input type='text' name='children_female' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Unspecified Male:</td>"."<td><input type='text' name='unspecified_male' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Unspecified Female:</td>"."<td><input type='text' name='unspecified_female' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Unspecified:</td>"."<td><input type='text' name='unspecified' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Voluntary Returns:</td>"."<td><input type='text' name='voluntaryreturns' required value=''></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Involuntary Returns:</td>"."<td><input type='text' name='involuntaryreturns' required value=''></td>";
echo "</tr>";
echo "</table>";
?>
</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" id="submit">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
<script>
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

// will run if 'update-mrcs-form' form was submitted
$(document).on('submit', '#add-ksa-form', function(e){
    var form_data=$('#add-ksa-form').serialize();
	console.log(form_data);
// submit form data to api
$.ajax({
    url: "createksarecord.php",
    type : "POST",
    //contentType : 'application/json',
    data : form_data,
    success : function(response) {
		if(response == 1){
        $("#success").html("KSA details were added!"); 
					$("#success").addClass("alert alert-success");
					var tablerow=(localStorage.getItem("tablerow"));
					
		var jsondata = $.parseJSON(form_data);//now json variable contains data in json format
		var myDataTable= $('#ksa').DataTable();
		myDataTable.row(tablerow).cell(':eq(0)').data(jsondata.center);
		myDataTable.row(tablerow).cell(':eq(1)').data(jsondata.registration_date);
		myDataTable.row(tablerow).cell(':eq(2)').data(jsondata.total);
		myDataTable.row(tablerow).cell(':eq(3)').data(jsondata.male);
		myDataTable.row(tablerow).cell(':eq(4)').data(jsondata.female);
		myDataTable.row(tablerow).cell(':eq(5)').data(jsondata.unaccompanied_minors);
		myDataTable.draw();
		}
		else{
      bootbox.alert("Invalid Query!");
     }
    },
    
});
     
    return false;
});
	
</script>
<script>
$('#registration_date').datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
//startDate: '-60d'
}).datepicker("setDate",'now');

$("#input_btn_calendar").click(function() {
    $("#registration_date").datepicker('show')
});
</script>