我相对较不熟悉使用php和ajax进行编程,所以请听我说。
这是我的jQuery / ajax代码:
function change2(radio){
section = $('input[type=radio][name=section]:checked').prop('id');
$.ajax({
url: './templates/teacherIndex4.php',
type: 'GET',
data: {'grade': grade, 'section': section},
dataType: 'json',
success: function (res) {
console.log(res);
if(res.status == "ok") {
document.getElementById("listStud").innerHTML = "Test";
}
},
error: function(error) {
console.log(error);
}
});
}
我已经检查了成绩和科目变量,它们还可以。 这是我的php代码:
if (isset($_GET['grade']) && isset($_GET['section'])) {
$grade = $_GET['grade'];
$section = $_GET['section'];
$recent_year = (int)date("Y");
$recent_month = (int)date("n");
if ($recent_month < 6){
$recent_year = $recent_year - 1;
}
$next_year = (int)$recent_year + 1;
$schoolyear = "S.Y. " . $recent_year . " - " . $next_year;
$sy = "select * from schoolyear where schoolYear = '$schoolyear'";
$syRes = mysqli_query($conn, $sy);
$schoolyearid = mysqli_fetch_array($syRes);
$schoolyear = $schoolyearid['schoolyear_id'];
$query = "select * from student where schoolyearid_fk = '$schoolyear' and gradeid_fk = '$grade' and sectionid_fk = '$section'";
$userinfo = mysqli_query($conn, $query);
$res = array();
while($line = mysqli_fetch_assoc($userinfo)){
$res[] = $line;
}
$res['status'] = "ok";
print json_encode($res);
}
我的问题是,它在错误函数中返回了它: console log image
我尝试将dataType更改为'text',它返回一个空响应,但是在成功函数中(试图获取res.status返回未定义)。
我也尝试过print json_encode($line)
和echo json_encode($line)
,它似乎返回了我一直在寻找的信息(在这里:data with crushed out real names)
有人可以帮我吗?这是我的个人研究,我不知道该如何解决。
编辑-function change2(radio)
之前的所有代码(在js文件中):
var grade;
function drop(x){
y = x.toString();
id = "id" + y;
$('#' + id).toggle("slow");
if ($('#' + y).hasClass('active')){
$('#' + y).removeClass('active');
} else {
$('#' + y).addClass('active');
}
}
function drop2(){
$('#subhand').toggle("slow");
if ($('#subhand').hasClass('active')){
$('#subhand').removeClass('active');
} else {
$('#subhand').addClass('active');
}
}
function change(radio) {
if (radio.checked && radio.id === "1") {
document.getElementById("sections").innerHTML = "<input type='radio' id='1' name='section' onclick='change2(this)'>Emerald <input type='radio' id='2' name='section' onclick='change2(this)'>Jade <input type='radio' id='3' name='section' onclick='change2(this)'>Ruby ";
grade = 1;
} else if (radio.checked && radio.id === "2"){
document.getElementById("sections").innerHTML = "<input type='radio' id='4' name='section' onclick='change2(this)'>Dahlia <input type='radio' id='5' name='section' onclick='change2(this)'>Kamia <input type='radio' id='6' name='section' onclick='change2(this)'>Rosal ";
grade = 2;
} else if (radio.checked && radio.id === "3"){
document.getElementById("sections").innerHTML = "<input type='radio' id='8' name='section' onclick='change2(this)'>Sodium <input type='radio' id='9' name='section' onclick='change2(this)'>Potassium <input type='radio' id='7' name='section' onclick='change2(this)'>Lithium ";
grade = 3;
} else if (radio.checked && radio.id === "4"){
document.getElementById("sections").innerHTML = "<input type='radio' id='11' name='section' onclick='change2(this)'>Neutron <input type='radio' id='10' name='section' onclick='change2(this)'>Electron <input type='radio' id='12' name='section' onclick='change2(this)'>Proton ";
grade = 4;
} else if (radio.checked && radio.id === "5"){
document.getElementById("sections").innerHTML = "<input type='radio' id='13' name='section' onclick='change2(this)'>Block A <input type='radio' id='14' name='section' onclick='change2(this)'>Block B <input type='radio' id='15' name='section' onclick='change2(this)'>Block C ";
grade = 5;
} else if (radio.checked && radio.id === "6"){
document.getElementById("sections").innerHTML = "<input type='radio' id='16' name='section' onclick='change2(this)'>Block A <input type='radio' id='17' name='section' onclick='change2(this)'>Block B <input type='radio' id='18' name='section' onclick='change2(this)'>Block C ";
grade = 6;
}
}
编辑2 -“网络”标签(在开发工具中):Network tab result in dev tools
答案 0 :(得分:0)
好的。已经有了解决方案。必须这样做:
$res = array();
while($line = mysqli_fetch_assoc($userinfo)){
$line["full_name"] = json_encode($line["full_name"]); //This line is the solution to the whole thing
$res[] = $line;
}
echo json_encode($res); //echo or print, didn't matter
简而言之,$ line [“ full_name”]必须经过json编码。打印出我想要的数据。 (谢谢那些帮助我解决了我的问题的人!)
答案 1 :(得分:-1)
您应该使用echo json_encode($ res);打印函数返回布尔值。您没有从php返回json数组。您正在返回布尔变量。这是你的问题。您的问题将通过使用echo json_encode($res);
来解决,它将返回json数组。