0

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

7
  • 1
    Can you elaborate? Where exactly are you seeing that the post request body is empty? Commented Oct 13 at 19:23
  • visitors.json file all post fields always empty but the request happens Commented Oct 13 at 19:27
  • 2
    You need to use multipart/form-data content type, not application/x-www-form-urlencoded. Commented Oct 13 at 19:36
  • Can you please paste full line exactly to replace? Commented Oct 13 at 19:39
  • 1
    @EliteX: This comment already tells you what to change. Have you tried that? Did that change not work as expected in some way? Commented Oct 13 at 19:42

3 Answers 3

6

You're sending the wrong content type when using FormData. Remove the Content-type header, and JavaScript will send the correct multipart/form-data header by default.

Sign up to request clarification or add additional context in comments.

17 Comments

tried did not work for me same empty post body
If you check the Network tab of the browser, do you see the body being sent there?
controller.php?type=visitorControl 200 xhr P00025.php:1819 0.8 kB 264 ms Yes it shows this in network body why i got downvote sir?
your request is visitor.php?type=visitorControl - yet you see controller.php?type=visitorControl - perhaps the issue is with different code
That's not what the body should look like.
That payload looks correct. I can't think of any reason why it's not being read properly by PHP.
i use cloudfare is that issue?
Have you tried something as simple as var_dump($_POST) to see what's being received?
|
2

If you're using a FormData object with XMLHttpRequest you do not set the Content-Type it will be set automatically for you

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.send(data);

Comments

1

When you specify a Content-Type of application/x-www-form-urlencoded, the XMLHttpRequest API is expecting that your post data be formatted just like query parameters. i.e.: ip=val1&ua=val2&country=val3. The following code should post to your script by first encoding the FormData entries into a URLSearchParams instance, then exporting that to a string.

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");
var params = new URLSearchParams(data);
xhr.send(params.toString());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.