Deliver PHP array from ajax to HTML field

Asked

Viewed 98 times

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

  • is a pretty big array to post here

  • But does it have fields? Is it an associative array? Or is it [0] => array("value1","value 2"), [1] =>array("outrovalor1","outrovalor 2")

  • OK, posted the exit in question

  • Right... And you need to insert the whole array into the right input value field? But then why use the json_encode and then use JSON.parse javascript if it is an Hidden input ?

  • You are converting the array to direct string, it will give error. Try: value='" . json_encode($relatoriosGr) . "'

  • 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 show object

  • exactly what is happening but envied. JSON.parse(result[1]) gives empty and only result[1] gives Object

Show 3 more comments

1 answer

1


Look, I think the best way for you to repurpose the input information would be like this:

php

$html .= "
        <input type='hidden' name='dados' id='dados' value='" . json_encode($relatoriosGr). "' />
        <button id='btnRelatorio' class='button'>Gerar Relatório</button>
    ";
echo $html;

javascript

success: function (result) {
    $(".resposta").html(result);
}

When the form is posted, just retrieve the information like this, in php or javascript:

php

$array = json_decode($_POST['dados'], true);

javascript

var str = $("#dados").val(); // pega a string em json
var obj = JSON.parse(str); // transforma em objeto (tipo de vetor)

In javascript you can send the string to php via ajax and transform into array with json_decode.

  • When I do echo $html; in PHP the code to. I had to encode again to proceed echo json_encode ($html); I did wrong?

  • I think your ajax expects an answer in json... Remove this part of the javascript that will work. I believe it is the line that contains the dataType... remove this line that will work

  • ok, I forgot about that. Thanks! but what kind of data I’m recbendp now but json. just to add there?

  • @Carlosrocha would be HTML! text/html but I don’t know how to insert this in ajax... I’ll see later and pass you.

  • @Carlosrocha is dataType:'html' ..... see here: https://answall.com/a/77358/15361

Browser other questions tagged

You are not signed in. Login or sign up in order to post.