Ajax不会显示在div标签中

时间:2016-10-27 08:24:02

标签: javascript jquery ajax

我可以通过发布Ajax来获取查询结果,但不是显示在mainInstructor2.php上的div标签中的结果,而是将其显示在另一个页面上,即作为InstructorStudent的操作中列出的页面。 PHP。任何帮助将不胜感激:)

这是我的HTML:

    <p id ="byStudent"> Search all the tutoring sessions by student</p>
        <form id="SearchByStudent" action ="InstructorStudent.php" method="post">
            Student Username <input = "textbox" type="text" name ="studentSearch" id="studentSearch" value="" required />
            <button class ="button" type="submit" name ="seachStudentSubmit" id ="searchStudentSubmit" value="submit"> Submit </button>
        </form>

        <div id ="searchByStudentContainer">
        </div>

这是我的javascript:

<script>

            $(SearchByStudent).submit(function(event){
                event.preventDefault();
                var form = $(this).serialize();
                var url = $form.attr('action');

                var posting = $.post( url, { studentSearch: $('input[name="studentSearch"]').val() } );

                posting.done(function(data) {
                    var content = data;
                    $(searchByStudentContainer).html(content);
                });
            });


    </script>

这是我的php:

<?php

    session_start();

    include_once("Connection.php");

    $studentSearch = $_POST['studentSearch'];

    $sql1 = "SELECT Students.StudentUsername 
            FROM Students
            INNER JOIN CLASS_STUDENT
            ON Students.StudentUsername = CLASS_STUDENT.StudentUsername
            INNER JOIN Class
            ON CLASS_STUDENT.ClassID = Class.ClassID
            INNER JOIN Instructors
            ON Class.InstructorID = Instructors.InstructorID
            WHERE Students.StudentUsername = '$studentSearch'";


    $result1= mysqli_query($link, $sql1);

    $resultarr = mysqli_fetch_assoc($result1);

    $studentSea = $resultarr['StudentUsername'];

    if(!$studentSea) {

       echo "   
        <script>
              alert ('Could not find student on file!');
              window.location ='mainInstructor2.php'
           </script>";
    }
    else {
    $sql2 = "SELECT Students.StudentUsername, CLASS_STUDENT.Year, CLASS_STUDENT.Semester, TUTOR_STUDENT.Date,
    TUTOR_STUDENT.TopicCovered, TUTOR_STUDENT.BeginTime, TUTOR_STUDENT.EndTime, TUTOR_STUDENT.TotalMinutes,
    TUTOR_STUDENT.Categories, TUTOR_STUDENT.TutorUsername
    FROM Students
    INNER JOIN CLASS_STUDENT
    ON Students.StudentUsername = CLASS_STUDENT.StudentUsername
    INNER JOIN TUTOR_STUDENT
    ON CLASS_STUDENT.StudentUsername = TUTOR_STUDENT.StudentUsername
    WHERE Students.StudentUsername = '$studentSearch'";

    $result= mysqli_query($link, $sql2);
            if ($result->num_rows > 0)
            {

                echo " 
                    <table>
                        <tr>
                            <th>Student Username</th>
                            <th>Year</th>
                            <th>Semester</th>
                            <th>Date</th>
                            <th>Topic Covered</th>
                            <th>Begin Time</th>
                            <th>End Time</th>
                            <th>Total Minutes</th>
                            <th>Category</th>
                            <th>Tutor Username</th>

                        </tr>
                            ";

                            // output data of each row
                while($row = $result->fetch_assoc()) 
                {
                        echo "
                            <tr>
                                <td>".$row["StudentUsername"]. "</td>
                         ";
                         echo "
                                <td>".$row["Year"]. "</td>
                         ";
                         echo "
                                <td>".$row["Semester"]. "</td>
                         ";
                         echo "
                                <td>".$row["Date"]. "</td>
                         ";
                         echo "
                                <td>".$row["TopicCovered"]. "</td>
                         ";
                         echo "
                                <td>".$row["BeginTime"]. "</td>
                         ";
                          echo "
                                <td>".$row["EndTime"]. "</td>
                         ";
                         echo "
                                <td>".$row["TotalMinutes"]. "</td>
                         ";
                          echo "
                                <td>".$row["Categories"]. "</td>
                         ";
                         echo "
                                <td>".$row["TutorUsername"]. "</td>
                         ";

                        echo "</tr>";
                                   }
                        echo "</table>";

                }
            else
                echo "This Student doesn't have any tutoring sessions.";



        mysqli_close($link);
    }

?>

3 个答案:

答案 0 :(得分:1)

尝试这个我想你错过了#

&#13;
&#13;
posting.done(function(data) {
                    var content = data;
                    $(#searchByStudentContainer).html(content);
                });
&#13;
&#13;
&#13;

答案 1 :(得分:0)

检查以下行:

<div id ="searchByStudentContainer"></div>

$(searchByStudentContainer).html(content);

searchByStudentContainer是id,你在引用时没有使用#。

所以改变这一行:

$('#searchByStudentContainer').html(content);

答案 2 :(得分:0)

  

document.getElementById(“searchByStudentContainer”)。innerHTML = content;

你可以试试这个

而不是

  

$(searchByStudentContainer)的.html(内容);