I'm having a problem with the $_POST varaible.
In this case I'm using the 8.1.18RC1 php version and Linux.
I have two files in php in the same folder.
contact.php
<h1>PHP Version <?php echo phpversion();?></h1>
<h2>OS: <?php echo php_uname('s');?></h2>
<h3 style='color: red;'>Form using JS</h3>
<form action="process-form.php" method="post">
<input type="hidden" name="token" value="whaou12hdi25qbdiqbdwq2541" />
<input type="text" name="name" />
<input type="submit" name="submit" value="send" id="submit-btn" />
</form>
<script>
var form = document.querySelector('form');
form.addEventListener('submit', function(event) {
event.preventDefault();
var formData = new FormData(form);
fetch('process-form.php', {
method: 'post',
body: formData
}).then(function(response) {
return response.json();
}).then(function(data) {
alert('Status: ' + data.status + '\n' + 'message: ' + data.msg);
});
});
</script>
process-form.php
<?php
$status = 0;
$msg = "No data";
if ($_POST){
$status = 1;
$msg = "Data ok";
}
$response = array(
'status' => $status,
'msg' => $msg
);
header('Content-Type: application/json');
echo json_encode($response);
?>
The problem is that the $_POST variable is arriving empty in the process-form.php file.
I didn't have the same problem in Windows OS. There it works correctly.
What could be happening in this case?