1

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

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
  • 2
    And your question is? Commented May 2, 2015 at 3:55
  • No data is being pulled at the moment. When I click on the submit button, nothing happens. If i type in the "mathcheck.php" link in my browser, I get an error saying "function get expects a string", meaning it wasnt pulling any data. Commented May 2, 2015 at 4:00
  • @NicholasRoberts Maybe try to put the radio buttons into the form ?! Also don't include your form into the validation: include('mathselect.php'); that makes also no sense Commented May 2, 2015 at 4:01

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

There may be other problems, but this is definitely one of the problems.
1

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.

Comments

0

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');

5 Comments

Thanks for the response. Sadly I'm still getting the same error when I click on the submit button. Data still isnt being sent....
Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything.
Fatal error: Function name must be a string in \\VSFILE07\STUDENTS-M-R\NICHOLAS.ROBERTS\SOCIAL_SOFTWARE\mathquiz\mathcheck.php on line 3 is caused by the $_POST syntax correction as noted by my answer and the one above.
@PedroLobito Well that wasn't all long sleep was it?
hahah, I was thinking that you would say that....SO is addictive, this answer is my last one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.