放置代码的位置,以便显示查询结果的详细信息

时间:2013-01-09 20:28:13

标签: php jquery ajax mysqli

我很难知道哪些代码被放置在哪些页面中。

我要做的是以下内容:

  • addstudentsession.php页面中,用户从#sessionsDrop下拉菜单中选择评估

  • 每次从下拉菜单中选择评估时(或者换句话说,每次在下拉菜单中更改选项),它都会执行ajax调用到单独的页面addedstudents.php来执行查询以查看所选评估中的哪些学生。

  • 从查询结果中获取评估的学生应显示在addstudentsession.php的多选框中(如果用户未选择评估,我仍然需要空白多选框显示在addstudentsession.php

我的问题是每个代码的放置应该在哪里才能执行此操作,因为我根本没有做到这一点。根本没有出现多选框,我收到了一个未定义的变量。

以下是当前的addstudentsession.php页面:

下拉菜单:

$sessionHTML = '';

if($sessionnum ==0){
$pHTML = "<span style='color: red'>Sorry, You have No Assessments under this Module</span>";
} 

$sessionHTML = '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;           

$studentInfo = array();

while ( $sessionqrystmt->fetch() ) {

$sessionHTML .= sprintf("<option value='%s'>%s - %s - %s</option>", $dbSessionId, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;    

}


$sessionHTML .= '</select>';

...

<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post' id='assessmentForm'>
<p><strong>Assessments:</strong> {$sessionHTML} </p>   
</form>";

多选框:

//getting undefined error below of studentSELECT
     $moduleexist="
        <form id='moduleExistForm'>
        <p><strong>Current Students in Chosen Assessment:</strong></p>
        <p>{$studentSELECT}</p>
        </form> 
        </div>";

        echo $moduleexist;

Ajax致电:

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

以下是执行查询的当前addedstudents.php页面,并在选择框中列出详细信息:

<?php

$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();   


$term = $_POST['studenttextarea'];


$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>';

?>

1 个答案:

答案 0 :(得分:0)

addedstudents.php中,您必须返回值$studentSELECT。添加

echo $studentSELECT

在PHP结束时。

您也永远不会在PHP代码中的任何位置使用$term = $_POST['studenttextarea'];。此外$sessionsdrop未定义,$stuentactive可能是拼写错误($studentactive)。 $currentstudentqry$currentassessmentqry相同。