0

Edit:

As per solution provided in the comments below.

The nothing happening in the Database
The solution was a simple refresh of the page as I previously had the delete.php file in a different directory (so the code worked fine... it was just a case of mistaken identity - it's always the littlest things).

The nothing happening on the page
The solution was giving the parent <div> a unique id (the same as the delete-button/image - 1) and then under the ajax-function in my jQuery.js file adding...

success: function() {
    $('#'.concat(id)).remove();
}

Thank you to @SikanderNawaz

End Edit

I'm trying to delete a row in my Database by using jQuery, AJAX and PHP, and my code, should be, but is not working.
I click the delete-button, but nothing happens, on the page or in the Database.

Where did I go wrong?
Hope someone here can help.

Database

------------------
| id | link      |
------------------
| 1  | image.jpg |
------------------

HTML

<head>
    ...
    <script src="jquery-3.4.1.js"></script>
    ...
</head>
<body>
    ...
    <button type="button" name="delete" id="delete" class="some stylings" value="1"><h6><b>X</b></h6></button>
    <img src="images/image.jpg" />
    ...
    <script src="jQuery.js"></script>
</body>

jQuery.js

$(document).ready(function(){
    $('button#delete').click(function(){
        var id = $(this).val(); // tested and returns 1 (see button value)
        
        $.ajax({
            url: 'delete.php',
            type: 'post',
            data: { id: id },
            success: function() {
                $('#'.concat(id)).remove();
            }
        });
    });
})

delete.php

<?php
    
    require ("connection.php");
    
    if (isset($_POST['id'])) {
        
        $id = $_POST['id'];
        
        try {
            
            $sql = 
            "
            DELETE FROM test
            
            WHERE
                id = ?
            ";
            
            $prepareTest = $pdo->prepare($sql);
            $prepareTest->execute([$id]);
            
        } catch(PDOException $e) {
            
            file_put_contents('../error/e.txt', $e->getMessage(), FILE_APPEND);
            
        }
    
    }
13
  • did you check your network tab? what error are you getting there? Commented Dec 28, 2020 at 7:57
  • kindly check network tab or console of your browser and show us what error is it throwing ? Commented Dec 28, 2020 at 8:00
  • yes... there is no error showing (from the connection.php or otherwise.... just nothing is happening when I click the delete-button - would have like to have shown connection.php too, just in case, but it said there was to much code) Commented Dec 28, 2020 at 8:00
  • hmmm.. it shows a 404 error... can't find delete.php - hang on... I'll look into it... if it is that simple I'm going to cry :) - but it should fond it since it's all in the same folder Commented Dec 28, 2020 at 8:04
  • nope... now I got it to show a 200-success message - it found the delete.php successfully but still nothing is happening - on the page or in the Database... so still not working... Edit: (forgot to refresh the page from wwhen I had delete.php in a separate folder) Commented Dec 28, 2020 at 8:10

1 Answer 1

1

While using JS/ Ajax always use console.log for logging the data and errors and spot the errors in Networks tabs as well for any db related errors and manage your debugging with data being dumped at each step of to spot the issue the error is coming from.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.