PHP AJAX更新无效

The Idea

  1. User will select examination date from dropdown list which is populated with data from the database.
  2. After selecting the date, the system will display the list of examiners based on the selected date.
  3. The user can now encode grades per student.
  4. After encoding the grade, the user will click the 'save' button which the system will save to the database. (Multiple update)

This is the code where the user selects the exam date.

<?php
include '../configuration.php';

$queryselect = mysql_query("SELECT examdateno, examdate from tbl_examdate ORDER BY examdate DESC");
?>
<!DOCTYPE html>

 <html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SPORT Qualifying Exam System</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <link rel="stylesheet" type="text/css" href="../css/component.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>         
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>

    <script>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                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("txtHint").innerHTML = xmlhttp.responseText;
                    }
                }                    
                xmlhttp.open("GET", "encodeinterviewajax.php?q=" + str, true);
                xmlhttp.send();

            }
        }
    </script>
</head>
<body>
    <header>
        <img src="../images/qes_logob.png" alt="logo">
        <button class="hamburger">&#9776;</button>
        <button class="cross">&#735;</button>
    </header>
    <div class="menu">
        <ul>
            <a href="encodeinterview.php">
                <li>Encode Grades</li>
            </a> 
            <a href="viewinterview.php">
                <li>View Grades</li>
            </a>               
            <a href="../index.php">
                <li>Logout</li>
            </a>  
        </ul>
    </div>


    <script>
        $(".cross").hide();
        $(".menu").hide();
        $(".hamburger").click(function () {
            $(".menu").slideToggle("slow", function () {
                $(".hamburger").hide();
                $(".cross").show();
            });
        });

        $(".cross").click(function () {
            $(".menu").slideToggle("slow", function () {
                $(".cross").hide();
                $(".hamburger").show();
            });
        });
    </script>

    <div id="content">
        <form>
                <h1>Exam Dates</>
                   <select name="examdate" id="examDate" onchange="showUser(this.value)">
                        <option>Select Exam Date</option>
                        <?php
                         while ($row = mysql_fetch_array($queryselect)) {
                            echo "<option value={$row['examdateno']}>{$row['examdate']}</option>
";
                        }
                        ?>
                    </select>
            </form>
        </div>
            <div id="txtHint">Examinees will be listed here</div>


</body>
</html>

This is where the displaying and the update should happen.

<?php
include '../configuration.php';
?>
<!DOCTYPE html>

<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/component.css" />
<link rel="stylesheet" type="text/css" href="../css/style.css">
<link rel="stylesheet" type="text/css" href="../css/grid.css">
</head>
<body>

<?php
$q = intval($_GET['q']);


$sql = mysql_query("select s.sno, s.fname, s.lname, s.examdate, s.interviewgrade, s.gwa from student s inner join tbl_examdate e on s.examdate=e.examdate where e.examdateno=$q");


?>
<div class="as_wrapper">    
    <div class="as_grid_container">
        <div class="as_gridder" id="as_gridder"></div> <!-- GRID LOADER -->
        <form method="post" action="">
            <table class="as_gridder_table">
                <tr class="grid_header">
                    <td><div class="grid_heading">Student No.</div></td>
                    <td><div class="grid_heading">First Name</div></td>
                    <td><div class="grid_heading">Last Name</div></td>
                    <td><div class="grid_heading">Exam Date</div></td>
                    <td><div class="grid_heading">Interview Grade</div></td>
                    <td><div class="grid_heading">GWA</div></td>
                </tr>

                <?php
                while ($row = mysql_fetch_array($sql)) {
                    ?>
                    <tr class="<?php
                    $i+=1;
                    if ($i % 2 == 0) {
                        echo 'even';
                    } else {
                        echo 'odd';
                    }
                    ?>">
                        <td><?php $sno[]=$row['sno'];echo $row['sno']; ?></td>
                        <td><?php $fname[]=$row['fname']; echo $row['fname']; ?></td>
                        <td><?php $lname[]=$row['lname'];echo $row['lname']; ?></td>
                        <td><?php echo $row['examdate']; ?></td>
                        <td><input type="text" size="3" maxlength="3" name="interview[]"></td>
                        <td><input type="text" size="3" maxlength="3" name="gwa[]"></td>
                    </tr>
                    <?php
                }
                ?>
                <tr>
                    <td colspan="6"><button id="btnUpdate">Save</button>
                </tr>
            </table>
        </form>
        <?php

        if (isset($_POST['btnUpdate'])){
            for($i=0;$i<sizeof($sno);$i++){
                $interview = $_POST['interview'][$i];
                $gwa = $_POST['gwa'][$i];
                $sql1=  mysql_query("UPDATE student SET gwa='$gwa', interviewgrade='$interview' where fname='$fname[$i]' AND lname='$lname[$i]' ");
                header('Location: encodeinterview.php');
            }
        }           
        ?>
    </div>
</div>
</body>

As I mentioned in my comment I'm not sure what exactly is not working for you but I can help you with a couple of things in what I'm guessing to be your homework project.

First, always wrap all jQuery actions that affect DOM elements or add EventListeners to DOM elements in a document.ready function. This function is triggered when all the HTML of the page has been loaded to the DOM. Adding an event listener or trying to ".hide()" and element before it is loaded to the DOM will fail. There is the long way of calling the document.ready function:

$(document).ready(function(){
    .... Code to manipulate the DOM goes here ...
});

and the shorthand function call that does exactly the same thing:

$(function(){
    .... Code to manipulate the DOM goes here ...
});

If you need to wait until all the additional resources (e.g. images) are loaded you can use:

$(window).load(function(){
    .... Your code goes here ....
});

This function is triggered when all resources are loaded. So if you're looking to get the position of an element or size of an element that contains images it's best to wait until the images are loaded. otherwise the position or size may well change once the images are loaded.

So your jQuery block needs to wrapped like this:

 <script>
    $(function(){
        $(".cross").hide();
        $(".menu").hide();
        $(".hamburger").click(function () {
            $(".menu").slideToggle("slow", function () {
                $(".hamburger").hide();
                $(".cross").show();
            });
        });

        $(".cross").click(function () {
            $(".menu").slideToggle("slow", function () {
                $(".cross").hide();
                $(".hamburger").show();
            });
        });
    });
</script>

And for goodness sake if your using jQuery NEVER type 'document.getElementById'. The jQuery equivalent is $('#id'). Note the '#' means ID where the '.' means CLASS. It's much shorter and it creates the element as a jQuery object which allows you to use all of jQuery's wonderful functions on it.

If you're using jQuery anyway. You should use it for AJAX. It's much easier. Here's your same showUser function done with jQuery including error handling for the ajax call:

function showUser(str) {
        if (str == "") {
            $("#txtHint").html("");
            return;
        } else {
            $.ajax({
                url: "encodeinterviewajax.php?q=" + str,
                type: 'GET',
                success: function(data){
                            $('#txtHint').html(data);
                         },
                error: function(jqXHR, textStatus, errorThrown){
                            console.log("Status = " + textStatus);
                            console.log("Error = " + errorThrown);
                         }
                });
        }