文档永远不会准备好从数据库中获取信息

时间:2011-12-08 00:12:05

标签: php javascript jquery mysql

基本上我遇到的问题是我有一段脚本被设置为在“评分”选择框发生变化时执行。基本上,文档中发生的事情永远不会准备好。当我使用Chrome的控制台打印出studentid和graderid值时,它们是空的。我把“退出();”在displayeval.php页面中运行,并且能够发现文档永远不会准备就绪。

我意识到很多这些代码都搞砸了,非常新手,但是我在一周结束之前就像一个疯狂的人一样完成了这个项目,我只想让它工作。

$(document).ready(function () {
    $('#graded').change(function () {
        var studentid = $('#studentid').val();
        var graderid = $(this).val();
        $.get("displayeval.php?graderid=" + graderid + "&studentid=" + studentid, function (data) {
            $('#behavior-290').val(data.comment);
        }, "json");
    });
});

instructoreval.php

    <?php
include('includes/header.php');
$student_id=$_GET['studentid'];
if($session->userlevel>=8)

//if they are an instructor
{
 if(isset($_POST['Submit'])){
      $query="SELECT * FROM Behavior b, Groups g WHERE g.GROUP_ID=" . $session->GROUP_ID . " AND b.CONTRACT_ID=g.CONTRACT_ID";
      $btwo = mysql_query($query) or die(mysql_error());
      $numB = mysql_num_rows($btwo);
      $query2="INSERT INTO Eval (STUDENT_ID, Grader_ID, GROUP_ID, Grade) VALUES (" . $_POST[graded] . ", " . $session->STUDENT_ID . ", " . $session->GROUP_ID . ", '10')";
      mysql_query($query2) or die(mysql_error());

      $evalid = mysql_insert_id();
      for($i=0;$i<$numB;$i++){ 
        $r2 = mysql_fetch_array($btwo);
        $query3="INSERT INTO EvalComment (CONTRACT_ID, BEHAVIOR_ID, Comment, EVAL_ID) VALUES (" . $r2[CONTRACT_ID] . ", " . $r2[BEHAVIOR_ID] . ", \"" . $_POST[$r2[BEHAVIOR_ID]] . "\", " . $evalid . ")";
        mysql_query($query3) or die(mysql_error());
      };
      $qfour = mysql_query("SELECT * FROM users WHERE GROUP_ID=" . $session->GROUP_ID . " AND STUDENT_ID=" . $_POST[graded]);
      $rfour = mysql_fetch_array($qfour);
      popup("Your comments for " . $rfour[lname] . ", " . $rfour[fname] . " have been submitted.");
    };


            $link = mysql_connect("localhost","drallen1","unicode") or die(mysql_error);
            mysql_select_db("drallen1");

            $qsix = mysql_query("SELECT * FROM users u WHERE u.GROUP_ID=" . $session->GROUP_ID . " AND NOT EXISTS(SELECT * FROM Eval e WHERE u.STUDENT_ID=e.STUDENT_ID) AND u.STUDENT_ID!=" . $session->STUDENT_ID);
            $numE = mysql_num_rows($qsix);
            /***************************************************
            //WHEN numE == 0 GO TO PIE CHART
            ***************************************************/
            //QUERY
            $qtwo = mysql_query("SELECT * FROM Behavior b, Groups g WHERE g.GROUP_ID=" . $session->GROUP_ID . " AND b.CONTRACT_ID=g.CONTRACT_ID");
             // match eval id
            $numB = mysql_num_rows($qtwo);

            if($numE>1)
              $page="evalform.php";
            else
              $page="evalprocess.php";

              echo "<form action=$page method=\"POST\">";?>

            <script type="text/javascript">
                $(document).ready(function(){
                            $('#graded').change(
                                function() {
                                var studentid = $('#studentid').val();
                                var graderid = $(this).val();

                                $.get( "displayeval.php?graderid=" + graderid + "&studentid=" + studentid,
                                   function(data){
                                        $('#behavior-290').val(data.comment);
                                   }, "json");
                            });
                    });
        </script>
            <input type="hidden" name="studentid" id="studentid" value="<?php echo $_GET['studentid'];?>" />
            Student: <select name="graded" id="graded">
              <option selected="selected">Please Select a Student to Grade</option>
              <?php for($i=0;$i<$numE;$i++){
                $rsix = mysql_fetch_array($qsix);?>
                <option value="<?php echo $rsix[STUDENT_ID]?>"><?php echo $rsix[fname] . " " . $rsix[lname]?></option>
              <?php };?>
            </select></br></br>

            <!--$qthree = mysql_query("SELECT EVAL_ID FROM Eval WHERE GRADER_ID=" . $student_id. " AND STUDENT_ID=" . graded.value ); -->

            <?php for($i=0;$i<$numB;$i++){ 
            //result of qtwo
              $rtwo = mysql_fetch_array($qtwo);
              echo "Behavior: <input name=\"BEHAVIOR_ID\" type=\"text\" value=\"" . $rtwo[BehaviorName] . "\" readonly=\"readonly\"/> </br>";

                //$queryshit="SELECT Comment FROM EvalComment WHERE EVAL_ID=RESULTFROMQTHREE AND BEHAVIOR_ID=" . $rtwo[BEHAVIOR_ID];
                //$comments;

              echo "Comments: <textarea name=\"" . $rtwo['BEHAVIOR_ID'] . "\" id=\"behavior-" . $rtwo['BEHAVIOR_ID'] . "\" rows=\"5\" cols=\"50\">". $comments . "</textarea> </br>"; ?>
            <?php };?>
            </br>
            <input type="submit" value="Send!" name="Submit"/>
          </form>
        </body>

   </html>

    <? include("includes/footer.php"); 
  }else{
  echo "You don't have access to this.";
};?>

displayeval.php

    <?php
include('include/session.php');


$grader_id=$_GET['graderid'];
$student_id=$_GET['studentid'];

$query="SELECT EVAL_ID FROM Eval WHERE GRADER_ID=". $grader_id . " AND STUDENT_ID=" . $student_id;

$result=mysql_query($query) or die(mysql_error());
$data=mysql_fetch_array($result);

$eval_id=$data['EVAL_ID'];

$query="SELECT BEHAVIOR_ID,Comment comment FROM EvalComment WHERE EVAL_ID=". $eval_id;
$result=mysql_query($query) or die(mysql_error());

$data2=mysql_fetch_assoc($result);
//print_r($data2);


// query database based on GET params

// fetch result
// $mysql_row = mysql_fetch_assoc()

// display in JSON: 
echo json_encode( $data2 );

这是displayeval(displayeval.php?graderid = 0&amp; studentid = 241654664)页面的结果

{"BEHAVIOR_ID":"1","comment":"Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write"}

1 个答案:

答案 0 :(得分:1)

首先,确保$('#graded')元素存在,请尝试:

$(document).ready(function() {
    alert($('#graded').attr('id'));   // should alert 'graded'
});

其次,如果您动态加载它(在页面呈现后),您需要通过change()$.bind()事件声明$.live()方法。

$('#graded').live('change', function () {
    ...
});

最后,请注意$.change()

对于复选框,您可能希望这样做:

$('#graded').change(function(){
    if ($(this).is(':checked')) {
        // the checkbox is now checked, do something
    } else {
        // the checkbox is now UN-checked, do something else
    }
});

除了所有这些(KISS原则)之外,确保实际上正在加载jQuery库:

if (jQuery) { 
   alert('jQuery is loaded');
}