如何比较动态创建的单选按钮的值

时间:2012-07-10 17:55:27

标签: php

我正在从MySQL动态生成一组单选按钮。按钮正在创建,分配给它们的变量正在填充,因为我做了一个echo print_r,它显示了变量的数组。我现在想要比较由此产生的值,如果该值为“0”,我想插入一个分数并显示绿色检查图形和单词正确。如果值为“1”,我希望它为分数输入不同的值并显示错误和红色X图形。这是我到目前为止(Everything动态填充问题和答案作为单选按钮):

<?php
echo '<form id="frmQuestion" name="frmQuestion" method="post" action="QuizQuestion1.php">';

// Connect to the Database
require_once('mysqli_connect.php');

//create the query for the question
$q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1";

//Create the query for the Answers
$q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1";

//Run the query
$r = mysqli_query($conn,$q);

//run the answer query
$r2 = mysqli_query($conn,$q2);

while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
    echo '<div id="Question1"><p> ' . $row['Question'] . '</div></p>';
}

//Declare the variables as a array
$AnswerResponse = array();
$AnswerStatusID = array();

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

    //Assign the AnswerStatusID to a var
    $AnswerStatusID[] = $row2['AnswerStatusID'];

    //Assign the AnswerResponse to a var
    $AnswerResponse[] = $row2['AnswerResponse'];
}

//Create the submit button 
echo '<input type="submit" value="Submit Answer" name="submit"/>';
echo '</form>';

//Logic for correct or incorrect answers
if (isset($_POST['q1']) && ($_POST['q1'] == '0'))
{
    //create the query for the score
    $q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')";   

    //Run the query 
    $r = @mysqli_query ($conn,$q3);

    if($r){

        //Confirm message data was entered with a correct response and a graphic
        echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>';
        echo '<a href="QuizQuestion2.php">Click here for the next question</a>';
    }
    else
    {
        //there was an error
        echo'<h1>System error</h1>';

        //Debugging message
        echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>';

    }//End of nested IF
}
else{
    //create the query for the score
    $q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')"; 

    //Run the query 
    $r2 = @mysqli_query ($conn,$q3);

    if($r2){

        //Confirm message data was entered with a correct response and a graphic
        echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/>';
        echo '<a href="QuizQuestion2.php">Click here for the next question</a>';
    }
    else
    {
        //there was an error
        echo'<h1>System error</h1>';

        //Debugging message
        echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>';

    }//End of nested IF

}

//Free up the results for the Question query
mysqli_free_result($r);

//Free up the results from the Answer query
mysqli_free_result($r2);

//close the DB connection
mysqli_close($conn); 

?>

1 个答案:

答案 0 :(得分:0)

这是答案,并按预期工作。感谢大家的投入。

//Declare the variables as a array
$AnswerResponse = array();
$AnswerStatusID = array();

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<div id="Question1"><input name="q1" type="radio" value="'.$row2['AnswerStatusID'].'"/>' . $row2['Answer'] . '</div><br/>';

//Assign the AnswerStatusID to a var
$AnswerStatusID[] = $row2['AnswerStatusID'];

//Assign the AnswerResponse to a var
$AnswerResponse[] = $row2['AnswerResponse'];
}




//Create the submit button 
 echo '<input type="submit" value="Submit Answer" name="submit"/>';
echo '<input type="hidden"name="submitted"value="TRUE"/>';
echo '</form>';



if($_POST['submitted']) { 

//Logic for correct or incorrect answers
if (isset($_POST['q1']) && ($_POST['q1'] == '0'))
{
//create the query for the score
$q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')";   

//Run the query 
$r3 = @mysqli_query ($conn,$q3);

//Confirm message data was entered with a correct response and a graphic
echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>';
echo '<a href="QuizQuestion2.php">Click here for the next question</a>';


}       
else{
//create the query for the score
$q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')"; 

//Run the query 
$r4 = @mysqli_query ($conn,$q4);

//Confirm message data was entered with a correct response and a graphic
echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/><br/>';
echo '<a href="QuizQuestion1.php">Click here to try again</a>';

}

}