I would like to click Delete, delete the record from the mySQL database .. Without giving reload to the page ... using jquery .. Does anyone know how to report? Thankful
I would like to click Delete, delete the record from the mySQL database .. Without giving reload to the page ... using jquery .. Does anyone know how to report? Thankful
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();
}
?>