Reputation: 1903
In my code I perform an ajax request and send this content:
Object {appointment_data: "{"id_services":["13","13"],"id_users_provider":"86…lable":false,"id":"133","id_users_customer":"87"}", customer_data: "{"first_name":"Mario","last_name":"Rossi","email":…:"","city":"","zip_code":"","notes":"","id":"87"}"}
appointment_data: "{"id_services":["13","13"],"id_users_provider":"86","start_datetime":"2015-11-19 13:43:00","end_datetime":"2015-11-19 14:55:00","notes":"","is_unavailable":false,"id":"133","id_users_customer":"87"}"
customer_data: "{"first_name":"Mario","last_name":"Rossi","email":"[email protected]","phone_number":"0000","address":"","city":"","zip_code":"","notes":"","id":"87"}"
NB: this content is included in appointment
variable that's json encode:
JSON.stringify(appointment);
Now from php side, inside the called function I'm trying to get the id
of the appointment in this way:
$_POST['appointment_data']['id'];
but I get this error:
Illegal string offset 'id'
I've also tried with .id
but the same appear.
NB: if I execute gettype()
I get string on $_POST['appointment_data']
maybe this is the problem? How I can fix this?
VAR DUMP PRINT
array(2) { ["appointment_data"]=> string(216) "{"id_services":["13","15","14"],"id_users_provider":"86","start_datetime":"2015-11-19 09:45:00","end_datetime":"2015-11-19 10:57:00","notes":"Appuntamento ","is_unavailable":false,"id":"131","id_users_customer":"87"}" ["customer_data"]=> string(146) "{"first_name":"Mario","last_name":"Rossi","email":"[email protected]","phone_number":"0000","address":"","city":"","zip_code":"","notes":"","id":"87"}" }
Upvotes: 1
Views: 49
Reputation: 219027
PHP isn't going to automatically convert your JSON string into an object. The combination of the HTTP POST and PHP just isn't that intuitive, and probably shouldn't try to be. Your biggest clue to the issue is your statement here:
if I execute
gettype()
I get string on$_POST['appointment_data']
In that case it's a string, and a string doesn't have an id
index. If you want to convert that JSON string into an object, PHP provides a way to do that:
$myObj = json_decode($_POST['appointment_data']);
At that point the value you're looking for should be available:
$myObj->{'id'}
Upvotes: 3