My insert ajax code will refresh the page but then not alert ("add successful"). Data does echo the right key trigger words ADD_OK i double checked. I have no errors. This seems odd to me because I used the exact same ajax code for both my add and delete.
Code Not Alerting But Adding to database
<script type="text/javascript">
$(document).on('click','#insert_btn',function (){
$.ajax({
type:'POST',
url:'add.php',
datatype: "json",
data: {
first_name: $("#first_name").val(),
last_name: $("#last_name").val(),
position: $("#position").val(),
date: $("#date").val(),
updated: $("#updated").val(),
},
success: function(data){
if (data=='ADD_OK') {
// alert(data);
location.reload();
alert("Add Successful");
} else {
//alert('something wrong');
}
}
})
});
</script>
Code Working doing both the delete and alert The same code for delete will refresh page and alert ("delete successful"). What would cause this?
<script type="text/javascript">
$(document).on('click','.delete_btn',function (){
if (confirm('Are you sure you want to delete this?')) {
var id = $(this).attr("id").match(/\d+/)[0];
var del_id = $('#example').DataTable().row( id ).data();
var del_id = del_id[0];
$.ajax({
type:'POST',
url:'delete.php',
data: {del_id:del_id},
success: function(data){
if (data=='DEL_OK') {
location.reload();
alert("Delete Successful!");
} else {
// alert('something wrong');
}
}
});
}
});
</script>
Add.php Code
<?php
$first_name = strtoupper($_POST['first_name']);
$last_name = strtoupper($_POST['last_name']);
$position = strtoupper($_POST['position']);
$date = $_POST['date'];
$updated = $_POST['updated'];
$stmt = $conn->prepare("INSERT INTO employees (first_name, last_name, position, date, updated) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param('sssss', $first_name, $last_name, $position, $date, $updated);
$add = $stmt->execute();
//print_r($_POST);
if($add) {
echo "ADD_OK";
}
?>