1

I am having trouble accessing information via PHP coming from json in javascript.

I use localstorage to save some temporary data:

var tbRomaneio = localStorage.getItem("tbRomaneio");// Recupera os dados armazenados
tbRomaneio = JSON.parse(tbRomaneio); // Converte string para objeto
if(tbRomaneio == null) { // Caso não haja conteúdo, iniciamos um vetor vazio
    tbRomaneio = new Array();
}

//Item
var medida = JSON.stringify({
    comprimento   : medidaComprimento,
    largura       : medidaLargura,
    token         : token.toString()
});
tbRomaneio.push(medida);
localStorage.setItem("tbRomaneio", JSON.stringify(tbRomaneio));

My code in javascript to post:

$.post('/pedido/salva-romaneio', {itens: localStorage.getItem("tbRomaneio")} )
.done(function(data) {
    //ok                                
})

So far so good. The problem is in PHP, when I read this information it returns error.

Here's my PHP code:

<?php
$itensRomaneio = json_decode($_POST['itens'], true);
print_r($itensRomaneio);
?>

Array
(
    [0] => {"comprimento":"230","largura":"54","token":"1495719950761"}
)

When I read the array I can not access the information, it gives the following error:

//Array
for($i = 0; $i < count($itensRomaneio); $i++) {

    echo $itensRomaneio[$i]->token;

}

Error:
<p>Severity: Notice</p>
<p>Message:  Trying to get property of non-object

And if I try to use it this way it only returns me this:

    //Array
    for($i = 0; $i < count($itensRomaneio); $i++) {

        echo $itensRomaneio[$i]['token'];

    }

Return only this:

" { "

If I give a print_r it is shown:

print_r($itensRomaneio[$i]);

//show
{"comprimento":"230","largura":"54","token":"1495719950761"}

What is going on?

2
  • Remove the 'true' flag on the json_decode Commented May 25, 2017 at 14:13
  • show print_r(_POST['itens']); Commented May 25, 2017 at 14:19

3 Answers 3

2

You're calling JSON.stringify() on each medida before you push it onto the tbRomaneio array. So you need to decode each element.

foreach ($itensRomaneio as $iten) {
    $iten = json_decode($iten);
    echo $iten->token;
}

But a better solution is to not encode each item, just the whole array.

var medida = {
    comprimento   : medidaComprimento,
    largura       : medidaLargura,
    token         : token.toString()
};
tbRomaneio.push(medida);
localStorage.setItem("tbRomaneio", JSON.stringify(tbRomaneio));

Then in PHP, you should not use true as the second argument to json_decode() if you want to get an array of objects instead of associative arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Solved my problem! ;)
1

Try:

for($i = 0; $i < count($itensRomaneio); $i++) {

    $values = json_decode($itensRomaneio[$i]);

    echo $values->token;

}

Comments

0

You $itensRomaneio is an array of json string. You need to decode them before you access the token property.

array_walk($itensRomaneio, function($v) {
    echo json_decode($v)->token;
}

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.