Php 使用ajax逐个向数据库提交循环值

Php 使用ajax逐个向数据库提交循环值,php,html,ajax,Php,Html,Ajax,我正在开发一个成绩管理系统,讲师可以登录并更新学生的成绩。 该系统要求提供该课程单元的所有学生,讲师更新分数。 我发现讲师点击一个按钮来提交每个学生的结果是非常单调的,但是他们是很多学生,并且每个学生身上都会出现很多按钮,然后我想到使用AJAX在“ONCHANGE call event”上提交结果 简单的AJAX脚本工作得很好,只不过它选择了循环中的最后一个学生,并按照他的id存储结果,即使您尝试为其他学生存储结果。它只会更新最后一个学生的结果 这是我给循环学生的代码 Marks.php <

我正在开发一个成绩管理系统,讲师可以登录并更新学生的成绩。 该系统要求提供该课程单元的所有学生,讲师更新分数。 我发现讲师点击一个按钮来提交每个学生的结果是非常单调的,但是他们是很多学生,并且每个学生身上都会出现很多按钮,然后我想到使用AJAX在“ONCHANGE call event”上提交结果

简单的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>";
?>

1st:在while循环中创建多个javascript函数是错误的方法。您需要创建单个函数并使用动态参数调用

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

//用于拾取值(标记)的Ajax脚本
函数输入结果(marksname、marksvalue、stdid、rowid){
if(marksname=='test'){
document.getElementById(stdid).innerHTML=“保存测试标记…”;
}否则{
document.getElementById(stdid).innerHTML=“保存考试分数…”;
}
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(xhttp.readyState==4&&xhttp.status==200){
if(marksname=='test'| | marksname=='tests'){
document.getElementById(stdid).innerHTML=xhttp.responseText;
}
}
}
xhttp.open('GET','../ajax\u calls/ajax\u call.php?marksname='+marksname+'&marksvalue='+marksvalue+'&rid='+rowid,true);
xhttp.send();
}

请格式化问题中的代码,使其可读
<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>