I have a PHP page that retrieves a list from the DB, and I want to display it on a loaded page using Ajax.
我有一个PHP页面从数据库中检索列表,我想使用Ajax在加载的页面上显示它。
Should I format it on the PHP side (HTML formatting), and just retrieve the data, or pass it to JS like dataA:dataB:dataC and format it client-side?
我应该在PHP端格式化它(HTML格式化),只检索数据,或者将它传递给JS,如dataA:dataB:dataC并将其格式化为客户端?
there won't be a lot of people using it, but I would like to know which is better (if there is a better method without taking the amount of users into account)
不会有很多人使用它,但我想知道哪个更好(如果有更好的方法而不考虑用户数量)
1 个解决方案
#1
1
Both will work fine. However in my opinion if you're gonna use ajax - and transfer information - a better practice will be to wrap the data in JSON format and parse it on the client's machine.
两者都可以。但是在我看来,如果你要使用ajax - 并传输信息 - 更好的做法是将数据换成JSON格式并在客户端的机器上解析它。
Example of the php output:
php输出示例:
{
"row1":{"field1":"value11", "field2":"value12"}
"row2":{"field2":"value21", "field2":"value22"}
...
}
Exmaple of parsing:
解析的例子:
$.ajax(...).done(function(result){
$.each(result, function(index,value){
$('#conatiner').append('<div>'+index+': field1='+value.field1+', field2='+value.field2+'</div>')
})
});
#1
1
Both will work fine. However in my opinion if you're gonna use ajax - and transfer information - a better practice will be to wrap the data in JSON format and parse it on the client's machine.
两者都可以。但是在我看来,如果你要使用ajax - 并传输信息 - 更好的做法是将数据换成JSON格式并在客户端的机器上解析它。
Example of the php output:
php输出示例:
{
"row1":{"field1":"value11", "field2":"value12"}
"row2":{"field2":"value21", "field2":"value22"}
...
}
Exmaple of parsing:
解析的例子:
$.ajax(...).done(function(result){
$.each(result, function(index,value){
$('#conatiner').append('<div>'+index+': field1='+value.field1+', field2='+value.field2+'</div>')
})
});