Remove Registration in jQuery

Asked

Viewed 672 times

0

I would like you to click Delete, delete the mysql database record.. No re-load the page... using jquery.. Can anyone inform? Grateful

  • 2

    You will need to make an Ajax request to a PHP that deletes the database record. Where exactly are you having difficulty?

  • Take a look at this answer: http://answall.com/q/70385/129 I think it has most of what you need. Explain later what you’re missing and you can’t do it.

1 answer

2


Example deleting a record:

Javascript:

$(document).on('click', '.delete-btn', function(){
    if(confirm('Are you sure?')){

        // Pega o id
        var product_id = $(this).closest('td').find('.product-id').text();

        // Gatilho para deletar o registro
        $.post("delete.php", { id: product_id })
            .done(function(data){
                console.log(data);

                // Mostra uma imagem de carregando
                $('#loader-image').show();

                // Re-carrega a lista de produtos
                showProducts();

            });
    }
});

PHP:

<?php
// Incluido para pegar a conexão com a base
include_once 'config/database.php';

try {

    $query = "DELETE FROM products WHERE id = ?";
    $stmt = $con->prepare($query);

    $stmt->bindParam(1, $_POST['id']);

    // Executa o SQL
    if($stmt->execute()){
        echo "Product was deleted.";
    }else{
        echo "Unable to delete product.";
    }
}

// Ao ocorrer erro
catch(PDOException $exception){
    echo "Error: " . $exception->getMessage();
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.