0
Well, I’m wearing it $.ajax
to recover a json_encode()
in an archive PHP.
In that file is populous one form:
$html .= "
<input type='hidden' id='dados' value='" . $relatoriosGr . "' />
<button id='btnRelatorio' class='button'>Gerar Relatório</button>
";
echo json_encode( $html );
The problem is here:
<input type='hidden' id='dados' value='" . $relatoriosGr . "' />
When at the end of this php file goes through json_encode it gives error because $relatoriosGr
is an array PHP.
If I try to play him like that:
echo json_encode( array ($html, $relatoriosGr) );
but when it comes to $.ajax back, I end up having trouble transferring this array encoded to the created field.
success: function (result) {
$(".resposta").html(result[0]);
$("#dados").val(JSON.parse(result[1]));
}
How to solve this?
ADD: array $relatoriosGr
in the PHP
Array
(
[17] => Array
(
[0] => Array
(
[data] => 2019-03-01
[ofetas] => 22.65
[decisoes] => 6
)
[1] => Array
(
[data] => 2019-03-03
[ofetas] => 55.33
[decisoes] => 3
)
[2] => Array
(
[data] => 2019-03-05
[ofetas] => 30.45
[decisoes] => 2
)
)
[18] => Array
(
[0] => Array
(
[data] => 2019-03-02
[ofetas] => 78.39
[decisoes] => 0
)
[1] => Array
(
[data] => 2019-03-05
[ofetas] => 30.00
[decisoes] => 1
)
)
)
How is this array? Could it show? Give a
print_r($relatoriosGr)
and show the result– Andrei Coelho
is a pretty big array to post here
– Carlos Rocha
But does it have fields? Is it an associative array? Or is it [0] => array("value1","value 2"), [1] =>array("outrovalor1","outrovalor 2")
– Andrei Coelho
OK, posted the exit in question
– Carlos Rocha
Right... And you need to insert the whole array into the right input value field? But then why use the
json_encode
and then useJSON.parse
javascript if it is an Hidden input ?– Andrei Coelho
You are converting the array to direct string, it will give error. Try:
value='" . json_encode($relatoriosGr) . "'
– edson alves
Not only that... He’s trying to insert an object into the value of an input... here:
JSON.parse(result[1])
this will give an error or just showobject
– Andrei Coelho
exactly what is happening but envied. JSON.parse(result[1]) gives empty and only result[1] gives Object
– Carlos Rocha