Due to some other issues I have replaced a "standard" POST request with JSON based POST request via Fetch API. After some help I got the server side running. However, as soon as I read an element from my results array, I receive an error.
POST REQUEST
fetch('/api/apicreatebooking.php', {
method: 'POST',
body: JSON.stringify({
'fromDate': document.getElementById('fromDate').value,
'toDate': document.getElementById('toDate').value,
'apptmnt': document.querySelector('input[name="unit"]:checked').value,
'total': document.getElementById("total").value,
'lan': document.getElementById("lan").value
})
})
.then(function(response){
return response.json()})
.then(function(data){
console.log(data)
});
SERVER SIDE PHP FOR POST REQUEST
$json = file_get_contents("php://input");
$data = json_decode($json);
$a="a>";
//if (isset($data['total'])) {$a += $data['total'];}
$a=print_r($data,true);
$result = array(
"success" => true,
"message" => $a,
"bookingCode" => '1234567'
);
$output = json_encode($result);
echo $output;
The code as stated above returns in the console
Object
bookingCode: "1234567"
message: "stdClass Object↵(↵ [fromDate] => 2022-07-02↵ [toDate] => 2022-07-09↵ [apptmnt] => 2↵ [total] => 11340 €↵ [lan] => DE↵)↵"
success: true
However, as soon as I swap the comment on the server side (if (isset...
) to be code
Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:8888/api/apibooking.php and Error: SyntaxError: The string did not match the expected pattern.