S
S
Sama Samsonov2018-04-21 20:40:01
PHP
Sama Samsonov, 2018-04-21 20:40:01

How to accept json data in a template?

I do a conclusion with the help of ajax.
From the template I pass ajax request in the controller accepted the request and output data from the database on request.
How can I return that data back to the template now?
controller code

$datesquery = Timesheet::find();
        $allhours = [];
        $json = Array();
        foreach ($datesquery as $countt) {
          $json[] = array(
            'id' => $countt->id,
            'date' => $countt->date,
            'start_time' => $countt->start_time,
            'stop_time' => $countt->stop_time,
            'allDay' => false
          );
        }
        return json_encode($json);  // здесь возвращает массив в шаблон

template code
<div id="date"></div>
        <div id="start"></div>
        <div id="stop"></div>

<script type="text/javascript">
    $("#credit").submit(function(e){
        e.preventDefault();

        var startmonth = $("#startmonth").val();
        var startyear = $("#startyear").val();

        $.ajax({
            type: "POST",
            url: "/user/history",
            data: { startmonth: startmonth, startyear: startyear}
        })
            .done(function(json) {
                var obj = JSON.parse(json);
/*  пробовал так не работает данные из контроллера возвращают массив 
                $("#start").text(obj.start_time);   // как мне принимать массив из контроллера
                $("#stop").text(obj.stop_time);   // чтобы вывести в цикле
                $("#date").text(obj.date);
*/
            });
    });
</script>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question