需要检查单选按钮以显示值

时间:2014-02-15 16:54:06

标签: php jquery ajax forms radio-button

我正在创建一个测验:

<?php
session_start();
include './connect.php';

if($_POST['chkAns']=="CHECK ANSWERS")
{
    $answer = mysql_query("SELECT num,answer FROM data");
    $count = mysql_num_rows($answer);
    while($ans = mysql_fetch_array($answer))
    {
        $stdAns = mysql_query("SELECT a".$ans["num"]." FROM account WHERE member='1'");
        $stdAnswer = mysql_result($stdAns,0,"a".$ans["num"]);
        if($ans["answer"]==$stdAnswer)
        {
        $score+=1;
        }
    }
    echo '<script>alert("Your Score is: "+'.$score.');</script>';
}
?>

<html>

    <head>

        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery-ui.min.js"></script>

        <script>
        $(window).load(function(){

            $("input[type='radio']").click(function(){
                var qstn = $(this).attr('qnum');
                var ans = $(this).attr('value');

                $.ajax({
                    type: "POST",
                    url: "./ans.php",
                    data: "stdID=2&q="+qstn+"&ans="+ans
                });

                alert("Question Number: " + qstn +"\nAnswer: "+ ans);        
            });
        });
        </script>

        <script language="javascript" type="text/javascript">
            $(document).ready(function() {

                $("#online").load("./tmr.php");

                var refreshId = setInterval(function() {
                    $("#online").load('./tmr.php');
                    $.ajax({
                       type: "POST",
                       url: "./update.php",
                       data: "stdID=1"
                    });
                }, 1000);

               $.ajaxSetup({ cache: false });

            });

        </script>

    </head>

    <body>
        <div id="online"></div>

            <?php
                $result = mysql_query("SELECT * FROM data ");
                $count = mysql_num_rows($result);

                while($rows = mysql_fetch_array($result))
                {
            ?>

            <p><?php echo $rows["question"]; ?></p>

            <?php
                $getAns = mysql_query("SELECT a".$rows['num']." FROM account WHERE member='2'");
                $answ= mysql_result($getAns,0,"a".$rows['num']);
                echo $rows['num'];

                if($answ=='A')
                {
            ?>

            <input checked type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="A" onClick="saveans(this);"/><?php echo"A. ".$rows['choice1']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="B" onClick="saveans(this);"/> <?php echo"B. " .$rows['choice2']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="C" onClick="saveans(this);"/> <?php echo"C. ".$rows['choice3']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="D" onClick="saveans(this);"/> <?php echo"D. " .$rows['choice4']; ?><br/>

            <?php } elseif($answ=='B') { ?>

            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="A" onClick="saveans(this);"/> <?php echo"A. " .$rows['choice1']; ?><br/>
            <input checked type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="B" onClick="saveans(this);"/> <?php echo"B. " .$rows['choice2']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="C" onClick="saveans(this);"/> <?php echo"C. " .$rows['choice3']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="D" onClick="saveans(this);"/> <?php echo"D. " .$rows['choice4']; ?><br/>

            <?php } elseif($answ=='C') { ?>

            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="A" onClick="saveans(this);"/> <?php echo"A. " .$rows['choice1']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="B" onClick="saveans(this);"/> <?php echo"B. " .$rows['choice2']; ?><br/>
            <input checked type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="C" onClick="saveans(this);"/> <?php echo"C. " .$rows['choice3']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="D" onClick="saveans(this);"/> <?php echo"D. " .$rows['choice4']; ?><br/>

            <?php } elseif($answ=='D') { ?>

            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="A" onClick="saveans(this);"/> <?php echo"A. " . $rows['choice1']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="B" onClick="saveans(this);"/> <?php echo"B. " . $rows['choice2']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="C" onClick="saveans(this);"/> <?php echo"C. " . $rows['choice3']; ?><br/>
            <input checked type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="D" onClick="saveans(this);"/> <?php echo"D. " . $rows['choice4']; ?><br/>

            <?php } else  { ?>

            <input  type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="A" onClick="saveans(this);"/> <?php echo"A. " . $rows['choice1']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="B" onClick="saveans(this);"/> <?php echo"B. " . $rows['choice2']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="C" onClick="saveans(this);"/> <?php echo"C. " . $rows['choice3']; ?><br/>
            <input type="radio" qnum="<?php echo $rows['num']; ?>" name="a<?php echo $rows['num']; ?>" value="D" onClick="saveans(this);"/> <?php echo"D. " . $rows['choice4']; ?><br/>

            <?php }
                } ?>

            <form method="POST">
                <input type="submit" value="CHECK ANSWERS" alt="chkAns" name="chkAns"/>
            </form>
        </body>
    </html>

问题是当我的字段为empty(a".$ans["num"])时,它只显示字母A而选项B,C和D为空白。但是如果该字段不为空,它将显示所有单选按钮并检查字段中保存的值。


更新

我刚刚编辑过它。这是由错误的拼写引起的 - 而不是choice2它是choi2ce

0 个答案:

没有答案