douchang8758 2012-08-18 02:47
浏览 49
已采纳

在提交之前修改$ _POST变量

I'm making a quiz generator, and I have an HTML form with radio buttons for multiple choice answers. Right now, when I submit my form, the contents of the $_POST variable looks like this when I submit it:

Array ( [a1] => Bob [a2] => Bobby )

(Bob and Bobby are the radio button choices I picked)

I'm generating this form using a PHP function, which returns an array of answers in addition to echoing the HTML form. Is there a way to modify the $_POST variable to have an 'answer' field (for checking my answers), like this:

 Array( [a1] => Bob [a2] => Bobby [answers] => Array( [0] => Bob [1] => Bilbo ))

The above was one way I thought of to check answer array with $_POST array.

Edit: More info on what I have so far:

  • I have a PHP function getTest() that echoes the HTML form, and returns an array with the correct answers.
  • getTest() generates each question randomly, and as such the correct answers are random.
  • The main problem is that I have two separate PHP files, questions.php and verify.php.
    • questions.php echoes the form using getTest(), and has the array of answers.
    • verify.php only has the contents of $_POST, BUT NOT the array of correct answers.

Is there a better way to check the results of the form submission in general? Thanks!

  • 写回答

6条回答 默认 最新

  • dongtuan5367 2012-08-18 02:51
    关注

    The best way to do a quiz is to have an answers array and a user input array. Loop through one and compare to the other using the same increment.

    You can take all of your post variables and create an array print_r($_POST); Then, loop through this.

    $inputArray = //the post data into an array
    $answerArray = array('a','b','a');
    $numCorrect = 0;
    for($a = 0; $a < count($inputArray); $a++)
    {
    if($inputArray[$a] == $answerArray[$a])
    {
    $numCorrect++;
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?