I have created a table which dynamically creates rows + a delete button everytime you add a row (the rows are read from the DB).
I want the delete button to remove the row and remove the DB data.
Here is my code:
Ajax call:
$(document).ready(function() {
$('button').click(function() {
var $this = $(this);
$.ajax({
type: 'GET',
url: 'ajaxManager.php',
data: 'idDel=' + $this.attr('id'),
success: function() {
$this.closest("tr").remove();
}
});
});
});
ajaxManager:
$name=filter_input(INPUT_POST, 'idDel', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
if($name !== FALSE) {
$result = deletePlayer($db, $name);
echo json_encode($result);
}
deletePlayer Function:
function deletePlayer(PDO $conn, $name) {
$sql = "delete from player where name = :name";
try {
$conn->beginTransaction();
$stmt = $conn->prepare($sql);
$stmt->bindValue(":name", $name, PDO::PARAM_STR);
$stmt->execute();
$conn->commit();
$players = getPlayerInfo($conn);
} catch (PDOException $exc) {
echo $exc->getMessage();
}
return $players;
}
The button is able to delete the row from the table, but it is not deleting the row from the DB.
What is wrong? :(
Thanks in advance!
Edit: the table name in the DB is called "player", the column inside is called "name".