Ajax function only returns error and does not call php function

Joined
Aug 6, 2022
Messages
2
Reaction score
0
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.
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);
 
}


?>
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
Please use the </> icon to paste code.
I would like to see the HTML. If it's too large - just the trimmed-down essential parts would do.

Have you checked that id=buttons.children[0].id does indeed give you what you want? It seems to me that $_POST['id'] does not exist or is empty.

When I call a PHP file for the first time I call a file that will return "Made It!", next I call a file that returns what I send it. Then I call the actual file.
 
Last edited:
Joined
Aug 6, 2022
Messages
2
Reaction score
0
Please use the </> icon to paste code.
I would like to see the HTML. If it's too large - just the trimmed-down essential parts would do.

Have you checked that id=buttons.children[0].id does indeed give you what you want? It seems to me that $_POST['id'] does not exist or is empty.

When I call a PHP file for the first time I call a file that will return "Made It!", next I call a file that returns what I send it. Then I call the actual file.
Thank you for answering! However, I realised my mistake. It was that I gave the function variable the name "$_POST", a reserved keyword in php so the php code could not run. Also appearantly I repeated the line
Code:
dataType: 'json',
twice.
Please use the </> icon to paste code.
I don't understand what you mean. I'm pretty sure I've already used the </> button for my code(?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,920
Messages
2,570,038
Members
46,449
Latest member
onedumbsquirrel

Latest Threads

Top