i’m building an app that asks you questions you can answer, i’m building it in react js, and my back-end is in php. the questions i’m showing comes from a database that php fetches and sends that in a json object to the javascript, this is successful. however, when i want to send the answers to the php in json format to save it in the database, the php is recieving an empty array, when i var_dump() the object it shows string(0) “” but in the console.log in javascript it is showing a successful php response with the correct json object.
this is very strange to me, i checked the json object in javascript before sending it to php and it is correct. but the $_POST php recieves is empty.
this is the php response i get in the javascript console.log:
string(700) "{"0":[{},{"done":false},{"answer":"","skip":true},{"answer":"NAETNAETNETN","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"abegegnwegn","skip":false},{"answer":"","skip":false},{"answer":"","skip":false}],"1":[{},{"done":true},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"","skip":false},{"answer":"SFBSFBsfb","skip":false},{"answer":"","skip":false}]}"
Done:
Answer: , Skip: 1
Answer: NAETNAETNETN, Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: abegegnwegn, Skip:
Answer: , Skip:
Answer: , Skip:
Done: 1
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: , Skip:
Answer: SFBSFBsfb, Skip:
Answer: , Skip:
Data received successfully`
but when I open the php in the browser I see this:
string(0) "" {"error":"Invalid JSON data"}
here is my code:
javascript function:
export const ExportJSON = async (jsonObject) => {
try {
// Send a POST request to the PHP script using async/await
const response = await fetch('http://my-IP-adress/documentation-chat/import.php', {
"method": 'POST',
"headers": {
'Content-Type': 'application/json',
},
"body": JSON.stringify(jsonObject),
});
// Check if the HTTP response is successful (status code 200-299)
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}, Message: ${await
response.text()}`);
}
// Parse the response text
const result = await response.text();
console.log(result); // Display the response from the PHP script
console.log(jsonObject)
return result;
} catch (error) {
console.error(error);
return error;
}
};
php file:
<?php
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
// If it's an OPTIONS request, send the necessary headers and exit
// If it's an OPTIONS request, send the necessary headers and exit
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('HTTP/1.1 200 OK');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');
exit();
}
// Read the raw POST data
$jsonData = file_get_contents('php://input');
var_dump($jsonData); // Log the received data
// Decode JSON with boolean values
$decodedData = json_decode($jsonData, true);
// Check if decoding was successful
if ($decodedData === null) {
// Handle JSON decoding error
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Invalid JSON data']);
exit();
}
// Iterate through the outer array
foreach ($decodedData as $section) {
// Access values in the inner array
$done = $section[1]['done'] ?? false;
// Example: Print the value from the outer array
echo "Done: $donen";
// Iterate through the questions
for ($i = 2; $i < count($section); $i++) {
// Access values in the question array
$answer = $section[$i]['answer'] ?? '';
$skip = $section[$i]['skip'] ?? false;
// Perform any necessary processing or storage of the data
// ...
// Example: Print the data for demonstration purposes
echo "Answer: $answer, Skip: $skipn";
}
}
// Send a response back to the client
echo 'Data received successfully';
?>