0

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?

DiChrist
  • 351
  • 7
  • 18
  • 1
    Does this answer your question? [php $\_POST array empty upon form submission](https://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission) – Christopher Apr 14 '23 at 14:04
  • Hey bro, I found the solution. It was the variable "max_multipart_body_parts" in php.ini file. What I do now? Close this question or put it as an answers. I didn't find the answer in the link you have send, but maybe it can be there. I found it searching in others places. – DiChrist Apr 14 '23 at 16:52
  • 2
    Glad you found a solution. You can [answer your question your own question](https://stackoverflow.com/help/self-answer). – Christopher Apr 14 '23 at 17:10
  • 1
    If you found the answer, you should write an Answer :-). See also [answer] – ADyson Apr 15 '23 at 00:19

0 Answers0