0

为了解决我遇到的困境,我正在尝试学习一段 ajax/jquery 代码,但我非常失败。

下面是我的ajax:

        $('#sessionsDrop').change( function(){
            var search_val = $(this).val();
            $.post("addstudentsession.php", 
            {studenttextarea : search_val}, 
            function(data){
   if (data.length>0){ 
     $("#studentselect").html(data); 
   } 

    });

目前,每次加载addstudentsession.php 脚本时,我都会得到一个空白页面。这是我正在处理的唯一脚本,所以我不确定是否应该将 ajax 链接到自身。但以下是我正在尝试做的事情:

我在下面有一个下拉菜单:

<select name="session" id="sessionsDrop">
<option value="">Please Select</option>
<option value='20'>EWYGC - 10-01-2013 - 09:00</option>
<option value='22'>WDFRK - 11-01-2013 - 10:05</option>
<option value='23'>XJJVS - 12-01-2013 - 10:00</option>
<option value='21'>YANLO - 11-01-2013 - 09:00</option>
<option value='24'>YTMVB - 12-01-2013 - 03:00</option>
</select> </p> 

下面我有一个多选框,其中显示了从上面的下拉菜单中进行选择评估的学生列表:

$studentactive = 1;

$currentstudentqry = "
SELECT
ss.SessionId, st.StudentId, st.StudentAlias, st.StudentForename, st.StudentSurname
FROM
Student_Session ss 
INNER JOIN
Student st ON ss.StudentId = st.StudentId
WHERE
(ss.SessionId = ? and st.Active = ?)
ORDER BY st.StudentAlias
";

$currentstudentstmt=$mysqli->prepare($currentassessmentqry);
// You only need to call bind_param once
$currentstudentstmt->bind_param("ii",$sessionsdrop, $stuentactive);
// get result and assign variables (prefix with db)

$currentstudentstmt->execute(); 

$currentstudentstmt->bind_result($dbSessionId,$dbStudentId,$dbStudentAlias,$dbStudentForename.$dbStudentSurname);

$currentstudentstmt->store_result();

$studentnum = $currentstudentstmt->num_rows();   

$studentSELECT = '<select name="studenttextarea" id="studentselect" size="6">'.PHP_EOL;      

if($studentnum == 0){

$studentSELECT .= "<option disabled='disabled' class='red' value=''>No Students currently in this Assessment</option>"; 


}else{   

while ( $currentstudentstmt->fetch() ) {

$studentSELECT .= sprintf("<option disabled='disabled' value='%s'>%s - %s s</option>", $dbStudentId, $dbStudentAlias, $dbStudentForename, $dbStudentSurname) . PHP_EOL; 
}

}

$studentSELECT .= '</select>';

但是我有一个小问题,当用户从下拉菜单中选择了一个选项时,我需要一种能够在选择框中显示学生列表的方法。php 代码的问题是必须提交页面才能找到其结果。

所以这就是为什么我试图使用 ajax 来解决这个问题,但我做错了什么?

4

1 回答 1

0

尝试使用 ajax 调用如下,

var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
    XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        XMLHttpRequestObject = false;
    }
}
}
$('#sessionsDrop').change( function(){
var search_val = $(this).val();
if (XMLHttpRequestObject) {

    XMLHttpRequestObject.open("POST", "addstudentsession.php", true);

    XMLHttpRequestObject.setRequestHeader('Content-Type',
            'application/x-www-form-urlencoded');

}
XMLHttpRequestObject.onreadystatechange = function() {
    if (XMLHttpRequestObject.readyState == 4
            && XMLHttpRequestObject.status == 200) {

        y = XMLHttpRequestObject.responseText;
                   $("#studentselect").html(y);


        }

};
};

XMLHttpRequestObject.send("studenttextarea=" + search_val);
于 2013-01-08T12:01:55.127 回答