2017-06-20 4 views
0

講師が学生の結果にログインして更新できる開発管理システムです。 システムは、そのコース単位を提供しているすべての生徒を正確にし、講師がマークを更新します。 私はそれが各学生のために結果を提出するボタンをクリックする講師のための非常に単調であることを発見し、まだ彼らは非常に多くの学生と抽出されたすべての学生に登場する非常に多くのボタンがあり、それから私はONCHANGEコール」に結果を提出するためにAJAXを使用するのではと思いましたイベント "です。ajaxを使用してループされた値をデータベースに1つずつ送信

簡単なAJAXのスクリプトが、あなたが他の学生のための保存結果を試しても、彼のIDを持つ行のループ格納結果の最後の学生を選んでいるだけ、ということうまく機能しています。最後の生徒の結果を更新するだけです。

ここにループされた学生のための私のコードです。

Marks.php

<?php 

$row=mysql_fetch_assoc($sql);//course unit information 

if(mysql_num_rows(mysql_query("SELECT * FROM allocations WHERE courseunitid='".$row['courseunitid']."' AND acid='".$_POST['acid']."' AND semester='".$_POST['semester']."' AND adminid='".$_SESSION['adminid']."'"))<1){ 

echo "<b><i><font color=red>Sorry! This course unit was not allocated to you in the specified search</font></i></b>"; 

}else{ 

echo"<hr size=1>"; 
echo "<table> 
<tr> 

    <td colspan=4><b>UPDATE RESULTS OF BELOW STUDENTS</b></td> 

</tr> 
<tr> 

    <td><b>NO</b></td> 
    <td><b>REG NO</b></td> 
    <td><b>NAME</b></td> 
    <td><b>TEST</b></td> 
    <td><b>EXAMS</b></td> 

</tr> 

"; 

$x=1; 

$sql_results=mysql_query("SELECT students.*,results.* FROM results INNER JOIN students ON results.stid=students.stid WHERE results.courseunitid='".$row['courseunitid']."' AND results.acid='".$_POST['acid']."' AND results.regsem='".$_POST['semester']."' AND results.adminid='".$_SESSION['adminid']."'"); 

while($rows=mysql_fetch_assoc($sql_results)){ 
?> 
<tr><td><?php echo $x++?> . </td><td> <?php echo strtoupper($rows['regno']) ?></td><td><?php echo ucwords($rows['fname']).'&nbsp;'.ucwords($rows['mname']).'&nbsp;'.ucwords($rows['lname']) ?></td><td><input type='text' name='test' value='<?php echo $rows['test'] ?>' style='width:40px;height:26px' maxlength='2' onblur='enter_results("test",this.value)'></td><td><input type='text' maxlength='3' name='exams' value='<?php echo $rows['exams']?> ' style='width:40px;height:26px' onblur='enter_results("exams",this.value)'></td><td id='<?php echo $rows['stid']?>'><?php echo $rows['stid'] ?></td></tr> 

<script> 

//Ajax Script for picking values (Marks) 
function enter_results(marksname,marksvalue){ 

    if(marksname=='test'){ 

     document.getElementById('<?php echo $rows['stid'] ?>').innerHTML="<font color=green><i>Saving test marks...</i></font>"; 

    }else{ 

     document.getElementById('<?php echo $rows['stid'] ?>').innerHTML="<font color=green><i>Saving exam marks...</i></font>"; 

    } 

    var xhttp=new XMLHttpRequest(); 

    xhttp.onreadystatechange=function(){ 

     if(xhttp.readyState==4 && xhttp.status==200){ 

      if(marksname=='test' || marksname=='exams'){ 

       document.getElementById('<?php echo $rows['stid'] ?>').innerHTML=xhttp.responseText; 

      } 



     } 

    } 

    xhttp.open('GET','../ajax_calls/ajax_call.php?marksname='+marksname+'&marksvalue='+marksvalue+'&rid=<?php echo $rows['rid']?>',true); 

    xhttp.send(); 

} 

</script> 

<?php 

} 

echo "</table>"; 
?> 
+3

あなたの質問のコードを読みやすいようにフォーマットしてください – Phil

答えて

0

第一:あなたはそれが間違ったアプローチだったwhileループ内で複数のJavaScript関数を作成します。単一の関数を作成し、動的パラメータで呼び出す必要があります。

onblur='enter_results("test",this.value,"<?php echo $rows[\'stid\'] ?>","<?php echo $rows[\'rid\'] ?>")'> 

:関数呼び出しの第三及び第四パラメータとしてstudent idrow idが追加されました。

PHP

<hr size=1> 

<table> 

    <tr> 

    <td colspan=4><b>UPDATE RESULTS OF BELOW STUDENTS</b></td> 

    </tr> 
    <tr> 

    <td><b>NO</b></td> 
    <td><b>REG NO</b></td> 
    <td><b>NAME</b></td> 
    <td><b>TEST</b></td> 
    <td><b>EXAMS</b></td> 

    </tr> 

    <?php 

    $x=1; 

    ?> 



    //while loop start here 
    <tr> 
     <td><?php echo $x++?> . </td> 
     <td> <?php echo strtoupper($rows['regno']) ?></td> 
     <td><?php echo ucwords($rows['fname']).'&nbsp;'.ucwords($rows['mname']).'&nbsp;'.ucwords($rows['lname']) ?></td> 
     <td><input type='text' name='test' value='<?php echo $rows['test'] ?>' style='width:40px;height:26px' maxlength='2' onblur='enter_results("test",this.value,"<?php echo $rows[\'stid\'] ?>","<?php echo $rows[\'rid\'] ?>")'></td> 
     <td><input type='text' maxlength='3' name='exams' value='<?php echo $rows['exams']?> ' style='width:40px;height:26px' onblur='enter_results("exams",this.value)'></td> 
     <td id='<?php echo $rows['stid']?>'><?php echo $rows['stid'] ?></td> 
    </tr> 

    //while loop end here 

    </table> 

Javascriptを:スクリプトは、whileループの外でなければなりません。

<script> 

    //Ajax Script for picking values (Marks) 

    function enter_results(marksname,marksvalue,stdid,rowid){ 


    if(marksname=='test'){ 

     document.getElementById(stdid).innerHTML="<font color=green><i>Saving test marks...</i></font>"; 

    }else{ 

     document.getElementById(stdid).innerHTML="<font color=green><i>Saving exam marks...</i></font>"; 

    } 

    var xhttp=new XMLHttpRequest(); 

    xhttp.onreadystatechange=function(){ 

    if(xhttp.readyState==4 && xhttp.status==200){ 

    if(marksname=='test' || marksname=='exams'){ 

     document.getElementById(stdid).innerHTML=xhttp.responseText; 

    } 



    } 

    } 

    xhttp.open('GET','../ajax_calls/ajax_call.php?marksname='+marksname+'&marksvalue='+marksvalue+'&rid='+rowid,true); 

    xhttp.send(); 



} 
</script> 
関連する問題