I am trying to get the value from json.stringfy sent to PHP file, for some reason php file is not receiving the key. If I manually add the key it is working fine. What could be wrong here:

My php file:

$request = json_decode(file_get_contents('php://input'), true);
$getID = $request['docid'];

$query = mysqli_query($con, "SELECT * FROM user_details WHERE id = $getID'");

if(mysqli_num_rows($query) > 0)
{
  $response["details"] = array();
  while ($row = mysqli_fetch_array ($query)) 
  {
                        // temp user array
                    $detail = array();
                    $detail["docname"] = $row["docname"];
                    $detail["textresults"] = $row["textresults"];


                    array_push($response["details"], $detail);
  }

  echo json_encode($response);

  $response["success"] = 1;
}
else
{
  $response["success"] = 0;
  echo json_encode($response);
}

This is my javascript file:

 function loadData() {

    var docid = window.localStorage.getItem('myKey');
    console.log("Docid " + docid);
    var xhr = new XMLHttpRequest();
            var url = "./api/getData.php";
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var json = JSON.parse(xhr.responseText);
                    console.log(json);
                }
            };

            var data = JSON.stringify({'docid': docid});
            xhr.send(data);

}

0 Answers