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

使用提交按钮将数据发送到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 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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题