簡體   English   中英

使用提交按鈕將數據發送到PHP腳本

[英]Using submit button to send data to PHP script

我正在進行數學測驗,用戶使用單選按鈕輸入他們的答案選項。 我想取單選按鈕的值,並將其與實際答案和增量分數進行比較,如果答案是正確的。

目前,我們正在測驗底部使用提交按鈕,我們正在嘗試發送“檢查答案”腳本的數據。 但是,我認為沒有正確提取任何數據。

這是單選按鈕的代碼,來自名為“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.\n<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>

而且,這里是“檢查答案”腳本的代碼,稱為“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;
}

因此,當我單擊表單的提交按鈕時,我會被指向帶有錯誤的mathcheck腳本。 以下是使用提交按鈕進行測驗的鏈接以供參考。 http://socialsoftware.purchase.edu/nicholas.roberts/mathquiz/mathselect.php?category=Calculus

將所有$_POST('x')更改$_POST['x']

就像現在一樣,你試圖像一個函數而不是數組一樣訪問它。

您正在加載$correctanswer一次。 每個問題都沒有改變? 因此,您需要在檢查該問題之前加載每個答案。

您的代碼可能有更多問題,我無法檢查所有內容,但您可以先將輸入字段放在form標記內,即:

<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>

此外, $_POST的正確語法是:

$_POST['test2'];

$_POST('test2');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM