0

I'm trying to save the data that "buchen.php" get in php variables but I keep getting the following error

Warning: Undefined array key "reise_id" in buchen.php on line 4

Warning: Undefined array key "platz_geb" in buchen.php on line 5

Can anyone tell what could be wrong here?

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'buchen.php');
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify({
        reise_id: "<?php echo $reise_id; ?>",
        platz_geb: platz_blau
    }));
//buchen.php

    $reise_id = $_POST['reise_id'];
    $platz_geb = $_POST['platz_geb'];
Teisan
  • 7
  • 3

1 Answers1

0

Instead of grabbing the POST variables as always, try the following on the PHP code:

$json = file_get_contents('php://input');
echo $json;
  • But why would this be necessary? – Jannick Breunis Sep 14 '22 at 13:29
  • I don't do PHP anymore, but as far as I could remember, file_get_contents() converts a file to a string, and you are passing php://input, which is basically the request body. So you are converting the JSON Object to string so you can work with it. – Fabio Barcelona Oct 29 '22 at 20:45