Answer the question
In order to leave comments, you need to log in
How to output an array of data via ajax to the Smarty template engine from php?
It is not possible to output an array of data to the foreach construct in the Smarty template engine.
Found out that it is necessary to use {literal}{/literal}. but when wrapping js in these tags, js doesn't work at all.
The site is made in mvc, that is, everything listed below is in different files.
In firebug I get the array
{"success":1,"message":"\u041a\u043e\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u0435 \u043d","$resultComment":[{"kurs_id":"7","comment":"dcfvg","user_id":"777"}]}Debug:
[success] => 1
[message] => Comment added
[$resultComment] => Array
(
[0] => Array
(
[kurs_id] => 7
[comment] => dcfvg
[user_id] => 777
)
)
)
You need to pass it to the .tpl file in the foreach
{foreach $resultComment as $item name=comment}
{$item["user_id"]}
{$item["comment"]}
{if $smarty.foreach.comment. iteration mod 1 == 0}
{/if}
{/foreach}
Using js, ajsx (file - main.js).
function addTOComment(itemId){
var comment = $('#comment').val();
var postData = {comment: comment};
$.ajax({
type: 'GET',
async: true,
url: "/comment/addtocomment/" + itemId + '/',
data: postData,
dataType: 'json',
success: function(data){
if(data['success']){
$('#addcom').prepend(data);
alert(data['message']);
} else {
alert(data['message']);
}
}
} );
}
Answer the question
In order to leave comments, you need to log in
Do you understand what you are asking?
If you need to dynamically render a template, then do it on the server, and return already rendered html in response to ajax, all that remains is to paste it in the right place.
And before you ask a question, try to at least a little delve into the topic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question