In fire bug I view my jquery post request parameters, its like this
adults 1
applicants[] [object Object]
attendees 1
children 0
In this post request the array called applicants contains json object which I want ti iterate over and pull out values in my codeigniter controller. The json string may look like this
({attendees:"2",
adults:"2",
children:"0",
grptype:"2",
'applicants[]':[{firstname:"John", lastname:"Doe", age:"33", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}, {firstname:"Jane", lastname:"Doe", age:"34", allergies:"true", diabetic:"false", lactose:"false", note:"pollen"}]
})
Look at the applicants[] above, see I have info for two people as a json object. I am not sure how access the data in my controller. see this
$applicants = $this->input->post('applicants');
$this->output->append_output("<br/>Here: " . $applicants[0].firstname );
I was thinking $applicants[0] woild refer to the json object and I could just pull outs values on demand. Not sure hat I am doing wrong. Thanks guys.
EDIT So I have adjusted my json and it looks like this
adults 2
applicants[] {firstname:"John", lastname:"Doe", age:"23", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}
applicants[] {firstname:"Jane", lastname:"Doe", age:"23", allergies:"false", diabetic:"false", lactose:"false", note:""}
attendees 2
children 0
Now I am still getting an error saying
**Message: json_decode() expects parameter 1 to be string, array given**
Any ideas ?
EDIT 2
Ok mu data now liiks like this
adults 1
applicants[] {"firstname": "John", "lastname": "Doe", "age": "34", "allergies": "true", "diabetic": "true", "lactose": "false", "note": "nuts"}
attendees 1
children 0
In the controller id did this
$applications = $this->input->post('applicants');
foreach ( $applications as $item)
{
$item = json_decode($item, true);
$this->output->append_output(print_r($item));
}
This is the result of that logic
Array
(
[firstname] => John
[lastname] => Doe
[age] => 34
[allergies] => true
[diabetic] => true
[lactose] => false
[note] => nuts
)
Not sure what I am doing wrong, no matter what I do to access the dater I get an error to the effect that I cannot access it like that. How do I pull out the values ?
[object Object]in the POST, means that is exactly what is going to be sent to the server - a string[object Object]. How are you creating the post data?