Like a lot of people here, I'm having an issue. I'm trying to delete something in an sql base with an html button (which is not in a form (should I?)). To achieve this I'm using Ajax and PHP. My Ajax code reach success and everything should work. But PHP is looking for an GET request so POST stay null.
Here is my Ajax:
function deleteImg(arg){
console.log("I'm now in the function");
var url = window.location.href;
$.ajax({
type: "POST",
url:url,
data:{action:'delete', id:'arg'},
beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")},
success:function(html) {
console.log("Success :) ");
}
});
}
And here is my php:
<?php
session_start();
$dir = '../uploads/';
$files1 = scandir($dir);
function remove($id){
$file = $dir . $files1[$id];
unlink($file);
}
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
echo "<script>alert( \" request method is get \" )</script>";
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<script>alert( \" La request method is post \" )</script>";
}
if(isset($_POST['action'])){
if( $_POST['action'] == 'delete'){
header('Location: projets.php');
remove($id);
}
}
?>
This is my first question and English is not my primary language, so I'm sorry if something is missing.