2

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 ?

4
  • That you see [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? Commented Sep 29, 2013 at 3:15
  • I have two forms in a widard and befor submit I create the json for each form and merge the json objects with jquery extend(). Then I send with jquert $.post(bla bla bla). Are t=you saing to send it accress as a string and not an object ? Commented Sep 29, 2013 at 10:54
  • Yes, you should convert the javascript object to a JSON string first and then send that string instead. Then, decode the JSON string on the server as @Sheikh Herra pointed out. Commented Sep 29, 2013 at 11:05
  • Can't believe I am so dumb, I used $item['firstname'] to pull out the value . Thanks guys for the guidance. Commented Sep 30, 2013 at 13:05

2 Answers 2

3

You have to decode it on the server using

$applications = json_decode($this->input->post('applicants'), true);

So, it'll become an associative array and you can use it like an array, without the second argument (true) in json_decode the json will be converted to an object. Until you decode it, it's only a string (json/java script object notation string).

Update: Since it's already an array of objects then you don't need to use json_decode, just loop the array in your view like

foreach($applicants as $item)
{
     echo $item->firstname . '<br />';
     echo $item->lastname . '<br />';
     // ...
}

According to the edit 2 it should be access as an array

echo $item['firstname'] . '<br />'
Sign up to request clarification or add additional context in comments.

3 Comments

I keep getting an error that json_decode needs a string and I am giving it an array. I did a print_r and it just out put "object Object". I am sending an array of json objects, should that array be strings instead ?
@Binaryrespawn, then you don't need to use json_decode, it's already an array of objects, just loop it.
I adjusted my json string as outlined by EDIT 2 above. And used php array notation instead of object notation like $items['firstname'] to pull out the values. that works. Thanks for the assist.
0

try this plz

$applicants = $this->input->post('applicants');
$json_output = json_decode($applicants );
foreach ( $json_output as $person)
{
  $this->output->append_output("<br/>Here: " . $person->firstname );
}

or

$json_output = json_decode($applicants,TRUE );
echo $json_output[0][firstname] ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.