用JSON打印sql表

我想打印所有的sql表并在html表中显示它们。我是新的PHP,JSON和AJAX。我成功发送用户名并在php中获得结果。我认为在JSON部分或AJAX中存在问题。谁能帮我?用JSON打印sql表

的index.php

<div class="col-lg-6"> 

<p id="usr" style="color:gray; font-size: 48px;"></p>

<script type="text/javascript">

var usr = document.getElementById("dom-target");

var username = usr.textContent;

username = username.trim().replace(/ /g, '%20');

document.getElementById("usr").innerHTML = username;

var sendtophp = "username="+username;

$.ajax({

type: "POST",

url: "getcoursetable.php",

data: sendtophp,

dataType:"json",

success: function(response) {

console.log(response);

var trhtml ='';

document.getElementById("demo").innerHTML = response;

$.each(response, function (i, item) {

trHTML += '<tr><td>' + item.cname + '</td><td>' + item.subject + '</td><td>' + item.course + '</td><td>'+ item.grade + '</td></tr>';

});

$('#results').append(trHTML);

}

});

</script>

<table id="results"></table>

</div>

getcoursetable.php

<?php 

include_once "connection.php";

if(isset($_POST["username"])){

$nick = $_POST["username"];

$prep = "SELECT * FROM `enrolledtable` WHERE nickname='$nick'";

$results = mysqli_query($con, $prep);

$jsonData = array();

while ($row = $results->fetch_row()) {

$jsonData[] = $row;

}

echo json_encode($jsonData);

}

?>

现在,我可以打印数据,但不喜欢的表,这样

<p id="demo">denemee,CS,300,B,denemee,CS,301,B ,denemee,CS,305,B ,denemee,CS,307,B,denemee,CS,408,A-,denemee,IE,208,B ,denemee,MATH,306,B</p> 

回答:

顶部宣布这个我解决我的问题。这可以采取在PHP表和创建HTML表。

的index.php

<div class="col-lg-6"> 

<p id="usr" style="color:gray; font-size: 48px;"></p>

<script type="text/javascript">

var usr = document.getElementById("dom-target");

var username = usr.textContent;

username = username.trim().replace(/ /g, '%20');

document.getElementById("usr").innerHTML = username;

var sendtophp = "username="+username;

$.ajax({

type: "POST",

url: "getcoursetable.php",

data: sendtophp,

dataType:"json",

success: function(response) {

var trhtml ='';

$.each(response, function (i, item) {

trhtml += '<tr><td>' + item[0] + '</td><td>' + item[1] + '</td><td>'+ item[2] + '</td></tr>';

});

$('.append').append(trhtml);

}

});

</script>

<table id="results">

<tr>

<th>Name</th>

<th>Subject</th>

<th>Course</th>

<th>Grade</th>

</tr>

<tbody class="append">

</tbody>

</table>

</div>

getcoursetable.php

<?php 

header('Content-Type: application/json');

include_once "connection.php";

if(isset($_POST["username"])){

$nick = $_POST["username"];

$prep = "SELECT subject,course,grade FROM `enrolledtable` WHERE nickname='$nick'";

$results = mysqli_query($con, $prep);

$jsonData = array();

while ($row = $results->fetch_row()) {

$jsonData[] = $row;

}

echo json_encode($jsonData);

}

?>

回答:

问题可能围坐这里:

 console.log(response); 

var trhtml ='';

document.getElementById("demo").innerHTML = response;

$.each(response, function (i, item) {

trHTML += '<tr><td>' + item.cname + '</td><td>' + item.subject + '</td><td>' + item.course + '</td><td>'+ item.grade + '</td></tr>';

});

$('#results').append(trHTML);

首先,JavaScript是区分大小写的语言:trhtmltrHTML不是相同的变量。第二,如果你的句子“输出php”意味着你报告了输出console.log(),那么response看起来像一个字符串给我,你必须将它解析为Json。

此外,我不知道字符串denemee的开头是什么,但它打破了Json表示法。

回答:

你的Ajax功能正在寻找类型的JSON数据,因此你需要在getcoursetable.php

header('Content-Type: application/json'); 

以上是 用JSON打印sql表 的全部内容, 来源链接: utcz.com/qa/257124.html

回到顶部