0

这是我的问题:

我有一个显示 X 类的动态列表。选择类时,将显示所有类的所有学生。当用户单击“提交”时,我想向数据库发送插入语句。提交按钮的代码在哪里?

我的尝试:伪代码/结构

放置在网络服务器上 1 个文件夹中的文件:Index.php 和 getStudentList.php

索引.php

<script>
function showStudents(str)
blabla
xmlhttp.open("GET","getStudentList.php?q="+str, true);
xmlhttp.send();
</script>
...
<div>
html display goes here, as well as the form itself
</div>

getStudentList.php

<script>
logic for javascript goes here which connects to a db and inserts attendance info
</script>
<?php
connect to db
query students in class
echo attendance form
?>

详细代码: Index.php

<!--start script for AJAX attendance display-->
<script>
function showStudents(str)
{
if (str=="") {
  document.getElementById("attendanceForm").innerHTML="";
  return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("attendanceForm").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getStudentList.php?q="+str,true);
xmlhttp.send();
}
<script>
<div>
    <?php
        //identify teacher's classes
        $school_connection = New school();
        $arrayTeachersClasses = $school_connection->queryTeachersClasses($_SESSION['teacherID']);
        //returns array of all classes taught
        //echoes today's date
        echo Date("l F d, Y");
        //create list so teacher can pick class
        echo "<form>";
        echo "<select name = \"courses\" onchange = \"showStudents(this.value)\">";
        echo "<option value=\"\">Select a class:</option>";
        $i = 0;//counter        
        while ($row = $arrayTeachersClasses[$i]) {
            echo "<option value=\"". $row[0] . "\">";
            echo $row[1] . "  ". $row[2] . "-L". $row[3] . " Room " . $row[4];
            echo "</option>";
            $i++;
        }
        echo "</select>";
        echo "</form>";


    /***
    //Sample "Select a Class" list
    <form>
    <select name="users" onchange="function(this.value)">
        <option value="">Select a class:</option>
        <option value="0">09:00:00  BEG-L1 Room303</option>
        <option value="1">TIME SUBJ-LEVEL ROOM</option>
        <option value="2">ditto</option>
        <option value="3">ditto</option>
    </select>
    </form>
    */

        $teacherID = $_SESSION['teacherID'];
        //echo $teacherID;


    ?>
</div>
<br />
<div id="attendanceForm">
    <p><b>Attendance List:</b></p>
    Please Select a Class
</div>

getStudentList.php

<script>
//Other JS stuff
$(document).ready(function() {

    //if submit button is clicked
    $('#submit').click(function () {        

    $i = 0;//counter
    var attendanceData = new Array();
    //Get the data for each student
    while ($row = $arrayStudentList[$i]) {
    var studentID = $('input[name=' . $row[0] . ']');
        if (studentID.val()=='') {
            studentID.addClass('highlight');
            return false;
        } else studentID.removeClass('highlight');

    $row[2] = studentID.val;
    $attendanceData[$i] = $row;
    $i++;
    }

    $row = $attendanceData[0];
    $record = $row[2];
    alert("My First JavaScript");

        //cancel the submit button default behaviours
        return false;
    }); 
}); 
</script>
4

1 回答 1

0

有很多方法可以做到这一点......下面是 2 个简单的解决方案:使用回发或通过 ajax。

选项 1 - 回发

  1. 单击提交时,将表单提交到索引页面本身:将表单操作留空或使用$_SERVER['PHP_SELF']
  2. 在 index.php 中显示任何内容之前检查请求类型和参数并保存数据
  3. 生成成功/失败消息并显示

选项 2 - Ajax

  1. 创建新页面insertStudent.php
  2. 添加 ajax 功能以提交表单并将表单发送到insertStudent.php. 请参阅jquery 提交或为您的项目重新发明轮子
  3. insertStudent.php保存学生并返回 json 状态或 (1/0)
  4. 处理 ajax 处理程序中的响应(步骤 2)以显示成功/失败消息

锄头这可以帮助您完成作业;-)

于 2012-12-07T20:14:35.273 回答