A
A
Alexander Valutin2019-09-24 17:07:04
PHP
Alexander Valutin, 2019-09-24 17:07:04

How to send array from PHP and display it in tpl template using JS in dynamically created fields?

PHP has an array:

Array
(
    [0] => Array
        (
            [0] => 1994-02-12
            [1] => 12021
            [2] => 1994-03-12 00:00:00
            [3] => 6.00
            [4] => 60.00
        )

    [1] => Array
        (
            [0] => 1994-01-25
            [1] => 25011
            [2] => 1994-03-25 00:00:00
            [3] => 25.00
            [4] => 5.00
        )

    [2] => Array
        (
            [0] => 2019-09-03
            [1] => 3092
            [2] => 2019-10-03 00:00:00
            [3] => 309.00
            [4] => 39.00
        )

    [3] => Array
        (
            [0] => 1963-04-13
            [1] => 13041
            [2] => 1963-05-13 00:00:00
            [3] => 1304.00
            [4] => 134.00
        )

    [4] => Array
        (
            [0] => 1964-07-25
            [1] => 25071
            [2] => 1964-09-25 00:00:00
            [3] => 2507.00
            [4] => 257.00
        )

)

It is necessary to send it to the tpl template, there I use smarty and with the help of JS I need to create dynamic fields with the values ​​of this array. I tried, at least, to get it in the template like this:
$smarty->assign('arrInvFix', $arrInvFix);
{if !empty($arrInvFix)}
                        {literal}
                        <script type="text/javascript">
                            var fixInvoice = new Array({$arrInvFix});
                            console.log(fixInvoice);
                        </script>
                        {/literal}
                    {/if}

how do I at least get the array on the template side?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Neverov, 2019-09-24
@TTATPuOT

{if !empty($arrInvFix)}
    <script type="text/javascript">
        //Формируем из массива JSON, который потом парсим через JSON.parse.
        let fixInvoice = JSON.parse("{htmlspecialchars(json_encode($arrInvFix))}");
        console.log(fixInvoice);
    </script>
{/if}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question