duanhuancong1969
duanhuancong1969
2015-05-02 03:54
浏览 96
已采纳

使用提交按钮将数据发送到PHP脚本

I'm making a math quiz where the user inputs their answer choice using radio buttons. I want to take the value of the radio button and compare it with the actual answer and increment score if the answer is correct.

At the moment, we are using a submit button at the bottom of the quiz where we are trying to send the data for the "check answers" script. However, I don't think any data is being pulled correctly.

Here is the code for the radio button which is from a form called "mathselect.php"

<?php //php starts here
require("opendbomath.php");
global $link, $DBname ;
if (!empty($_GET)) {
    $category = $_GET["category"];
   $sql = "SELECT * FROM `math`"; 
   $result = mysqli_query($link,$sql);
   $numOfQuestions = "SELECT COUNT(*) FROM `math`"; 
   $queryNumOfQuestions = mysqli_query($link,$numOfQuestions);
   if ($queryNumOfQuestions=mysqli_query($link,$sql))
{
    // Return the number of rows in database
    $rowcount=mysqli_num_rows($queryNumOfQuestions); //this is number of total rows
    printf("Result set has %d rows.
<br><br>",$rowcount);
}
$list = array();
$questionNumber = 0;

while (count($list) < 10){
    do { //this is where we pull questions from database
    $randomInt = rand(2,$rowcount); //random int is inbetween 2 and       numbers of rows
    $sqlselect = "SELECT * FROM `math` WHERE `bid` =  \"" . $randomInt   . "\" AND `acategory` = \"" . $category . "\"  ";
    $sqlSelectQuery = mysqli_query($link,$sqlselect);
    $numOfGoodQuestions = mysqli_num_rows($sqlSelectQuery);
    } while ($numOfGoodQuestions == 0); //if it returns blank field, it will loop again

    if (in_array($randomInt,$list)){ //if it pulls duplicate number, continue
        continue;
        } 
    else {
        //use fetch function
        $row=mysqli_fetch_array($sqlSelectQuery);
        $questionNumber = $questionNumber + 1;
        $strQuestionNumber = (string)$questionNumber;
        $slots = array();
        array_push($slots,$row['aanswer']); //pushes answer choices into slots array
        array_push($slots,$row['awrong1']);
        array_push($slots,$row['awrong2']);
        array_push($slots,$row['awrong3']);
        shuffle($slots); //shuffles the answer choices
?>
<form action="mathcheck.php" method="post">
<?php
print ("<input type = 'radio' name='test".$strQuestionNumber."' value   ='$slots[0]'>".$slots[0]."<br>"); //displaying 4 radio buttons with value of answer choice
        print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[1]'>".$slots[1]."<br>");
        print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[2]'>".$slots[2]."<br>");
        print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[3]'>".$slots[3]."<br>");
        print("<br>");

    }
}
//submit buton will go here
?>
<input type ="submit" value = "submit">
</form>

And also, here is the code for the "check answer" script which is called "mathcheck.php"

<?php
include('mathselect.php');
$answerChoice1 = $_POST('test1'); //pulls value of radio button
echo $answerchoice1;
$answerChoice2 = $_POST('test2');
$answerChoice3 = $_POST('test3');
$answerChoice4 = $_POST('test4');
$answerChoice5 = $_POST('test5');
$answerChoice6 = $_POST('test6');
$answerChoice7 = $_POST('test7');
$answerChoice8 = $_POST('test8');
$answerChoice9 = $_POST('test9');
$answerChoice10 = $_POST('test10');

$correctAnswer = $row['aanswer'];
$score = 0;

if ($answerchoice1 == $correctAnswer){
    $score = $score + 1;    
} else {
$score = $score;
}

if ($answerchoice2 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice3 == $correctAnswer){
    $score = $score + 1;
} else {
$score = $score;
}

if ($answerchoice4 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice5 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice6 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice7 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice8 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice9 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

if ($answerchoice10 == $correctAnswer){
    $score = $score + 1;
} else {
    $score = $score;
}

So, when I click on the submit button of the form, I get directed to the mathcheck script with an error. Here is the link to the quiz with the submit button for reference. http://socialsoftware.purchase.edu/nicholas.roberts/mathquiz/mathselect.php?category=Calculus

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglang2010
    donglang2010 2015-05-02 04:06
    已采纳

    You may have more problems on your code, I couldn't check everything but you may start by putting the input fields inside the form tag, i.e.:

    <form action="mathcheck.php" method="post">
    <?php
    print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[0]'>".$slots[0]."<br>"); //displaying 4 radio buttons with value of answer choice
            print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[1]'>".$slots[1]."<br>");
            print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[2]'>".$slots[2]."<br>");
            print ("<input type = 'radio' name='test".$strQuestionNumber."' value ='$slots[3]'>".$slots[3]."<br>");
            print("<br>");
    
        }
    }
    ?>
    <input type ="submit" value = "submit">
    </form>
    

    Also, the correct syntax of $_POST is:

    $_POST['test2'];
    

    NOT

    $_POST('test2');
    
    点赞 评论
  • douye9175
    douye9175 2015-05-02 04:02

    You are loading $correctanswer once. It's not changed for every question ? SO, you need to load each answer before you do the check for that question.

    点赞 评论
  • duanluan8390
    duanluan8390 2015-05-02 04:07

    change all of your $_POST('x') to $_POST['x']

    as it stands now you are trying to access it like a function not an array.

    点赞 评论

相关推荐