I'm currently using a combination of HTML, PHP, Javascript, and AJAX in order to create an HTML , populate it from a table in a MySQL database, then add a inside of the table in order to both remove the row, and delete the entry from the MySQL database as well. This is what I currently have:
<?php
echo '<table id="tag_table" border=1>
<tr>
<th>Tags</th>
<th>Delete Tag</th>
</tr>';
$iter = 0;
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
echo "<tr id=" . $iter . "\">";
echo "<td>" . $row['name'] . "</td>";
echo "<td>";
echo "<button id=\"delete\">Delete</button>";
echo "</td> </tr>";
}
?>
Now what I want to do is add .click() functionality to the delete button, but I'm stuck because I want to delete the row using the following Javascript:
function deleteTag(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
}
But I also need to delete the corresponding entry in the MySQL database, requiring PHP (which I can't use inside the javascript file). If anyone has a possible solution or idea of how to accomplish these things, I'd be so grateful to know.
Thanks for any help.
Edit 1. My PHP code:
<?php
$db = new PDO('my info here bla bla bla');
$query = "DELETE FROM Tags WHERE name=" . $_POST['name'];
$db->exec($query);
$db = null;
?>
idvalue, which is invalid. Aside from what, what you're asking is basically how to use AJAX. You don't "use PHP in JavaScript", but instead make an AJAX request to a PHP script on the server which would delete from the database. There are many AJAX tutorials available to get you started. You tagged the question with jQuery, which has plenty of examples for simple AJAX operations.