在显示所选选项时遇到麻烦

时间:2013-01-08 07:50:11

标签: php javascript jquery ajax

为了解决我的困境,我正在努力学习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来解决这个问题,但是我做错了什么?

1 个答案:

答案 0 :(得分: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);