在那里,我使用此代码使用HTML
创建JSON
表,在我的网络程序中,我使用JSON
生成PHP
,现在我需要传递{{ 1}}作为上述函数的参数,我该怎么做。
JSON
我可以使用<script type="text/javascript">
var data = [
{
"UserID": 1,
"UserName": "rooter",
"Password": "12345",
"Country": "UK",
"Email": "sac@gmail.com"
},
{
"UserID": 2,
"UserName": "binu",
"Password": "123",
"Country": "uk",
"Email": "Binu@gmail.com"
},
{
"UserID": 3,
"UserName": "cal",
"Password": "123",
"Country": "uk",
"Email": "cal@gmail.com"
},
{
"UserID": 4,
"UserName": "nera",
"Password": "1234",
"Country": "uk",
"Email": "nera@gmail.com"
},
{
"UserID": 4,
"UserName": "nera",
"Password": "1234",
"Country": "uk",
"Email": "nera@gmail.com"
}
];
$(document).ready(function () {
var html = '<table class="table table-striped">';
html += '<tr>';
var flag = 0;
$.each(data[0], function(index, value){
html += '<th>'+index+'</th>';
});
html += '</tr>';
$.each(data, function(index, value){
html += '<tr>';
$.each(value, function(index2, value2){
html += '<td>'+value2+'</td>';
});
html += '<tr>';
});
html += '</table>';
$('body').html(html);
});
</script>
JSON
值作为参数传递给此函数
答案 0 :(得分:0)
您可以使用php标记传递参数
<?php
$data = '[
{
"UserID": 1,
"UserName": "rooter",
"Password": "12345",
"Country": "UK",
"Email": "sac@gmail.com"
},
{
"UserID": 2,
"UserName": "binu",
"Password": "123",
"Country": "uk",
"Email": "Binu@gmail.com"
},
{
"UserID": 3,
"UserName": "cal",
"Password": "123",
"Country": "uk",
"Email": "cal@gmail.com"
},
{
"UserID": 4,
"UserName": "nera",
"Password": "1234",
"Country": "uk",
"Email": "nera@gmail.com"
},
{
"UserID": 4,
"UserName": "nera",
"Password": "1234",
"Country": "uk",
"Email": "nera@gmail.com"
}
]';
?>
<script>
$(document).ready(function () {
var html = '<table class="table table-striped">';
html += '<tr>';
var flag = 0;
var data2 = <?php echo $data; ?>;
$.each(data2[0], function(index, value){
html += '<th>'+index+'</th>';
});
html += '</tr>';
$.each(data2, function(index, value){
html += '<tr>';
$.each(value, function(index2, value2){
html += '<td>'+value2+'</td>';
});
html += '<tr>';
});
html += '</table>';
$('body').html(html);
console.log(html);
});
</script>
答案 1 :(得分:0)
我建议只在php中创建你的表,这样当它加载时它会在页面上,对于seo和用户体验来说会更好。
但是这就是当你从php加载页面时你可以打印javascript数据而不必发出ajax请求。
关键是<?= json_encode(array); ?>
json encode会将php数组/关联数组解析为json对象。
我在其单独的脚本标记中执行此操作,以防出现解析或编辑语法错误。
<?php
$data = [
[ "UserID" => 1, "UserName" => "rooter", "Password" => "12345", "Country" => "UK", "Email" => "sac@gmail.com" ],
[ "UserID" => 2, "UserName" => "binu", "Password" => "123", "Country" => "uk", "Email" => "Binu@gmail.com" ],
];
?>
<script>data = <?= json_encode($data, JSON_NUMERIC_CHECK); ?></script>
$(document).ready(function () {
var html = '<table class="table table-striped">';
html += '<tr>';
var flag = 0;
$.each(data[0], function(index, value){
html += '<th>'+index+'</th>';
});
html += '</tr>';
$.each(data, function(index, value){
html += '<tr>';
$.each(value, function(index2, value2){
html += '<td>'+value2+'</td>';
});
html += '<tr>';
});
html += '</table>';
$('body').html(html);
});
</script>