gpt4 book ai didi

javascript - 如何从 PHP 中提取 JSON 数据并将其显示在数据表中?

转载 作者:行者123 更新时间:2023-12-03 07:18:04 24 4
gpt4 key购买 nike

我想使用 PHP 将报告转换为 JSON 格式并使用 AJAX JQuery 显示,从而从数据库中获取报告。但我似乎无法正确传递数据。有人可以告诉我我的错吗?

这是我的代码 View :

$(document).ready(function() {

$("#searchOverall").click(function() {


var ay = $("#Overall_acadyear").val();
var year = $("#Overall_year").val();

if (ay === undefined || ay == '') {

alert("Select Academic year.");

} else if (year === undefined || year == '') {

alert("Select year level.");

} else {


$.ajax({

url: "js/overallreport.php",
dataType: "json",
data: "ay=" + ay + "&year=" + year,
success: function(data) {
//left this blank because I am not sure of what I am doing.
//I used $.getJSON and $.each
},

error: function() {
alert('Cannot retrieve data from server.');
}

}); //ajax




} //else

}); //btnOverall



});
//This is the JS FILE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="form-group col-sm-4">
<select class="form-control" id="Overall_acadyear">
<option value='' active>Select AY</option>
<?php $r->selectAcademicYear(); ?>
</select>

<select class="form-control col-sm-4" id="Overall_year" align="right">
<option value='' active>Select year level</option>
<?php $r->selectYear(); ?>
</select>
<div class="form-group">
<button type="button" class="control-label btn-success" id="searchOverall" name='createAccount'>Search</button>
<button type="reset" class="control-label btn-danger" id="reset">Clear Fields</button>
</div>

</th>

</tr>
<tr>
<th>Student Number</th>
<th>Student Name</th>
<th>Section</th>
<th>Status</th>
</tr>
</thead>
<tbody id="tableBody">

</tbody> <!--This is the view file -->

<?php
require($_SERVER['DOCUMENT_ROOT']."/finalsis/include/config.php");
include_once($_SERVER['DOCUMENT_ROOT']."/finalsis/include/class.utility.php");
header("Content-Type: application/json");
$ay = $_GET['ay'];
$year = $_GET['year'];

$ay = $obj->cleanString($ay);
$year = $obj->cleanString($year);


$conn = mysqli_connect(db_server,db_user,db_password,db_database);
$ay = mysqli_escape_string($conn,$ay);
$year = mysqli_escape_string($conn,$year);

$selectSQL = "SELECT studentlevel_student, student_fname, student_mname, student_lname,
section_name, student_status FROM tblstudentlevel inner join
tblstudent on studentlevel_student = student_number inner join
tblyearsection on studentlevel_ys = ys_id WHERE studentlevel_acadyear = '" .$ay."' AND year_name = '" .$year. "'";
$result = mysqli_query($conn,$selectSQL);

$output = '{"student": [';
while($rs = mysqli_fetch_array($result)){
$name = $obj->getFullName($rs['student_fname'],$rs['student_mname'],$rs['student_lname']);
$output .= '{"sno":"' .$rs['studentlevel_student']. '", ';
$output .= '"name":"' .$name. '", ';
$output .= '"section":"' .$rs['section_name']. '",';
$output .= '"status":"' .$rs['student_status']. '"},';
}

$output .= "]}";
mysqli_close($conn);
echo json_encode($output);

//This is where my data gathering happens. ?>

最佳答案

问题出在以下行:

echo json_encode($output);

json_encode 需要一个数组。您正在传递字符串。

$jsonData = array();
while($rs = mysqli_fetch_array($result)){
$name = $obj->getFullName($rs['student_fname'],$rs['student_mname'],$rs['student_lname']);

$jsonData[] = array(
'sno' => $rs['studentlevel_student'],
'name' => $name,
);
}

echo json_encode($jsonData);

关于javascript - 如何从 PHP 中提取 JSON 数据并将其显示在数据表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com