I want to send a POST request to my PHP file to handle it and store the data in the database.. I'm pretty stuck at it since $_POST stays empty whatever I try..
Can someone help me sending a post request and help me how to handle it ?
My axios request:
// Performing a POST request
axios.post('dev/api/post.php',{ text: text, unk: unk})
.then(function(response){
console.log(response);
}).catch(function (error) {
console.log(error);
});
And this is what I kinda tried in PHP, some code is commented out because not sure if it worked..
if(isset($_POST) && !empty($_POST)){
/*
$jsonReceiveData = json_encode($_POST);
$json_output = json_decode($jsonReceiveData);
$task = $json_ouput->text;
$uniquekey = $json_output->unk;
*/
echo $_POST;
/*
$stmt = $pdo->prepare("INSERT INTO todo (id, tekst, uniquekey) VALUES ('', :tekst, :unk)");
$stmt->bindParam(':unk', '1');
$stmt->bindParam(':tekst','testing');
$stmt->execute();
*/
}
SOLUTION:
$data = json_decode(file_get_contents("php://input"), true);
$task = $data['text'];
The object was found in php://input