Search code examples
javascriptphpxmlhttprequest

Waning: Undefined array key when trying to store post variables (xmlhttprequest)


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'];

Solution

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

    $json = file_get_contents('php://input');
    echo $json;