0

I have a JSON object which looks like this:

{"name":"name","description":"description","picture1":"link","picture2":"link","picture3":"link"}

It is send to a php-file like this:

var request = new XMLHttpRequest();
    var phpfile = "http://mayar.abertay.ac.uk/~1605524/coursework/createVenue.php";
    request.open("POST", phpfile, !0);
    request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

    request.onreadystatechange = function(){

        if(this.readyState == 4) {
            if(this.status == 200) {
                window.alert("Success");
            }
        }
    }
    request.send(json);

I try to get the content of the JSON object in PHP like this:

header('Content-type: application/json');
$json = file_get_contents('php://input');   
$json_decode = json_decode($json ,true);

$venuename = $json_decode -> name;
$description = $json_decode -> description;
$pictureone = $json_decode -> picture1;
$picturetwo = $json_decode -> picture2;
$picturethree = $json_decode -> picture3;

The data should be written in a database and it work before as I passed the data as parameters over the url, but now it creates an database entry with empty fields. I think my mistake is accessing JSON object in PHP, but I can't find a solution.

SOLUTION

Data access has to be done like this:

$venuename = $json_decode[name];
Felix
  • 9
  • 1
  • 8

0 Answers0