getElementById()返回Null

时间:2012-09-26 08:49:33

标签: php javascript jquery mysql ajax

我需要在页面末尾显示结果。当我尝试使用getElementById()添加反馈时,它总是返回null,我猜它超出了范围。你有任何想法如何解决这个问题吗?

这是我的JavaScript功能。

 function handler(var1,quizId,isCorrect,score,name,email) {
        alert(var1);
        //var id = parseInt(quizId);
        quizId++;
        var points=10;

        if(isCorrect=='true'){
            score=score+points;
            document.getElementById("result").innerHTML="Correct<br/>";
            var string_url="quiz.php?qusId="+quizId+"&score="+score+"&name="+name+"&email="+email;
            setTimeout(function() {
  window.location = string_url
}, 5000);
        }
        else{
            document.getElementById("result").innerHTML="Wrong<br/>";
            var string_url="quiz.php?qusId="+quizId+"&score="+score+"&name="+name+"&email="+email;
            setTimeout(function() {
  window.location = string_url
}, 5000);
            }

}

这是我的PHP代码。

<body >
<?php 
$qusId=$_GET['qusId'];
$passscore=70;



if(isset($_GET['score'])){
    $score=$_GET['score'];
}
else{
    $score=0;
}
if(isset($_POST['name'])){
    $name=$_POST['name'];

}
else{
    $name=$_GET['name'];
}

if(isset($_POST['email'])){
    $email=$_POST['email'];
}
else{

    $email=$_GET['email'];
}
?>
<form action="email.php?name=<?php echo $name; ?>&email=<?php echo $email;?>&score=<?php echo $score;?>" method="POST">


   <?php
            $result = select("SELECT * FROM questions WHERE question_id='$qusId'");
            //$row = mysql_fetch_array($result);


        ?>
<?php
$i=$_GET['qusId'];

if($qusId<11){



while($row = mysql_fetch_array($result))
{
?>
<table width="581" height="175" border="0"  align="center">
<tr>
<td><h4><?php echo $i.'.' .$row['questions']; ?></h4>
<?php $i++; ?>
</td>
</tr>
<tr>
<td>
<?php $qId=$row['question_id'];?>
<?php

$result1=select("SELECT * FROM answers WHERE questionId='$qId' ORDER BY RAND()");
while($row1=mysql_fetch_array($result1)){

    ?><input type="radio" name="answers" value="<?php echo $row1['answers'];?>"  onclick="handler('<?php echo $row1["feedback"]; ?>',<?php echo $qusId;?>,'<?php echo $row1["isCorrect"]; ?>',<?php echo $score;?>,'<?php echo $name; ?>', '<?php echo $email; ?>')
"/ ><?php echo $row1['answers']; ?><br/>







<?php 
} ?>
&nbsp;</td>
</tr>

</table>
</form>
<p>
  <?php
}
}
else{

    ?>
</p>
<p>

<table>
<tr><td>
Your Name:<?php echo $name;?><br /><br/>
Your Email:<?php echo $email;?><br /><br/>
Pass Score:<?php echo $passscore;?><br /><br/>
Your Score:<?php echo $score;?><br/><br/>



<?php if($score>=$passscore){?>
Result:Congratulation! You Have passed the questionnaire.<br/><br/
<?php }else{ ?>
Result:Sorry! Please <a href="/uaquiz/index.php">Try again</a><br/>
<?php } ?>

 </p>
    </td></tr>
    <tr><td><input class="submit" type="submit" value="Send Result"  /></td></tr>
    </table>
    </form>

   <table id="feedback">
<tr>
<td id="questions">1.   What is Union Assurance brand pay off line<br/>
2.  What does the Union Assurance brand colour orange signify<br/> 
3.  What does the Union Assurance brand colour grey signify<br/>
4.  Union Assurance is baked by <br/>
5.  Union Assurance Brand is built on the platform of <br/>
6.  Union Assurance brand promise trust is delivered through <br/>
7.  Union Assurance Symbol depicts<br/>
8.  What’s Union Assurance brand ranked in the LMD Brands.<br/>
9.  What’s Union Assurance brand value?<br/>
10. What is the best way to build your brand <br/> </td>
<td id="result">aaa</td>
</tr></table>


    <?php

    }

?>
</body>

1 个答案:

答案 0 :(得分:0)

getElementById()将始终返回null。因为没有“结果”ID可用。

因为php脚本条件

if($qusId<11){}else{}

你需要把这一行  <td id="result">aaa</td>

在php脚本的末尾,或者您可以在if条件中添加此行而不是在else部分。