I was able to link to the database to add data via AJAX "GET", but now I must send another AJAX "POST" request to delete the data from the SQL database. Given the fact that I have no prior knowledge of PHP or SQL, I am struggling mightily with this task. I have tried to send the request using many different parameters, but am finding these attempts to be futile. I have found many tutorials on this using jQuery, but unfortunately I must use plain JavaScript.
PHP:
<?php
$userName = "root";
$password = "";
$dbName = "ToDo";
$server = "localhost";
$db = new mysqli($server, $userName, $password, $dbName);
$sql = "DELETE FROM tasks WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $_REQUEST["id"]);
$stmt->execute();
$returnVal = $stmt->affected_rows;
$stmt->close();
$db->close();
echo $returnVal;
JavaScript: (userWaste represents text input added to database via client side js)
const del = (userWaste) => {
let xhr = new XMLHttpRequest();
let url = "ToDo/deleteTask.php";
let params = "?id=" + userWaste;
xhr.open("POST", url, true);
xhr.onreadystatechange = () => {
if(xhr.readyState == 4 && xhr.status == 200){
let deletedTask = xhr.responseText;
}
}
xhr.send(params);