I have a few dynamic php hyperlink elements used as buttons which are supposed to delete data without refreshing the page. I have been working on the Ajax for two weeks and still hadn't found any solution. I'm so fed up. The Ajax function only returns the error and does not even call the php function(I even changed it to a non-function code and still doesn't work). Please help me figure this out.
PHP function:
JavaScript:
$(document).ready(function(){
let buttons = document.querySelectorAll('.rmvElement');
for (let i=0; i<buttons.length; i++) {
buttons[i].addEventListener('click', function(event){
event.preventDefault;
clickFunc(i);
});
}
My Ajax page:
function clickFunc(i) {
id=buttons[i].children[0].id
$.ajax({
type:'post',
url:'./backend/adminAccess/deleteItem.php',
dataType: 'json',
data:{id:id},
cache: false,
dataType: 'json',
success:function(data){
console.log("success");
console.log(data);
},
error: function(err) {
console.log("Error");
console.log(err);
}
});
}
});
PHP function:
PHP:
<?php
if(isset($_POST['id'])) {
deleteBack($_POST);
}
function deleteBack($_POST){
$start=14;
$mid=strpos($_POST['id'],'-');
$il=substr($_POST['id'],$start);
$len=strlen($il);
$idSpecific=substr($_POST['id'],$mid+1);
$slen=strlen($idSpecific);
$id=substr($_POST['id'],$start,$len-$slen-1);
echo "<script>console.log(100);</script>";
// var_dump(100) ;
echo json_encode($id);
}
?>