Always getting empty post data null in php end
var data = new FormData();
data.append('ip', 'val');
data.append('ua', 'val2');
data.append('country', 'val3');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'visitor.php?type=visitorControl', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
PHP END
if ($_GET['type'] == 'visitorControl') {
$file = 'visitors.json';
$visitors = json_decode(file_get_contents($file), true);
$userid = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0,6);;
$newEntry = [
'userip' => $_POST['ip'],
'useragent' => $_POST['ua'],
'country' => $_POST['country'],
'status' => 'Pending',
'timestamp' => 'NA'
];
$visitors[$userid][] = $newEntry;
file_put_contents($file, json_encode($visitors));
// setcookie("valid_userid", $userid, time()+86400);
$_SESSION['valid_userid'] = $userid;
echo json_encode(array(
'userid' => $userid,
'status' => 'ok'
));
}
[{"userip":null,"useragent":null,"country":null,"status":"Pending","timestamp":"NA"}],"LInUxe":[{"userip":null,"useragent":null,"country":null,"status":"Pending","timestamp":"NA"}],"MweWyG":[{"userip":null,"useragent":null,"country":null,"status":"Pending","timestamp":"NA"}],"qfAFcG"]
Even i tried stringify data to json and change request header but always getting empty
Can anyone help
multipart/form-datacontent type, notapplication/x-www-form-urlencoded.