0

So here's my code in getting an input from a user on how many questions (multiple choice) he/she would like to make:

       Multiple choice: <input type = "text" name="MC"><br>

       <input type = "submit" name = "confirm" value = "Confirm">

After that, this is the code of how many questions the system will generate:

<?php

if(isset($_POST['confirm'])){

$MC = $_POST['MC'];
echo "<form method = 'POST' name = 'items' action ='createquestions.php'>";
$items = 1;

    for ($x = 1; $x <= $MC; $x++) {

        echo "Question Number $items:"; echo "<input type = 'text' name = 'questions[]' style='width: 500px'><br><br>";
        echo "A. "; echo "<input type = 'text' name = 'ans1[]'>";
        echo "B. "; echo "<input type = 'text' name = 'ans2[]'><br>";
        echo "C. "; echo "<input type = 'text' name = 'ans3[]'>";
        echo "D. "; echo "<input type = 'text' name = 'ans4[]'><br>";
        echo "Correct Answer: "; echo "<input type = 'text' name ='cans[]'><br><br>";
        $items++;

    }
        echo "<input type ='submit' name = 'save' value = 'Save'>";
        echo "</form>";
}
?>
<?php

The problem is that it will only save the last input of the user. For example, I have inputted 2 in the Multiple choice: --textbox here-- This code will generate 2 questions, 8 choices, 2 cans = correct answer but it will only save the 2nd question, answers, and the correct answer. the system won't get the record of the 1st question, answer, and the correct answer.

Here is the code where I would insert it on the database:

<?php

    if(isset($_POST['save'])){

        $user_id = $_SESSION['id'];
        $questions = $_POST['questions']; 
        $ans1 = $_POST['ans1'];
        $ans2 = $_POST['ans2'];
        $ans3 = $_POST['ans3'];
        $ans4 = $_POST['ans4'];
        $cans = $_POST['cans'];


        foreach($questions as $q){
            echo "<input type = 'hidden' value = '$q'>";
        }

        require_once('xcon.php');

        $query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans) 
              VALUES ('NULL','$user_id','$q','$ans1','$ans2','$ans3','$ans4','$cans')";
    $result = mysql_query($query);

    if($result){
        echo 'Insert Success!';
    }
    else{
        echo 'Error';
    }

}

?>
jaredk
  • 986
  • 5
  • 21
  • 37
Ger Mel
  • 45
  • 5

3 Answers3

0

According to this post you should use:

echo "Question Number $items:"; echo "<input type = 'text' name = 'questions' style='width: 500px'><br><br>";
echo "A. "; echo "<input type = 'text' name = 'ans[]'>";
echo "B. "; echo "<input type = 'text' name = 'ans[]'><br>";
echo "C. "; echo "<input type = 'text' name = 'ans[]'>";
echo "D. "; echo "<input type = 'text' name = 'ans[]'><br>";
echo "Correct Answer: "; echo "<input type = 'text' name ='cans'><br><br>";

And this:

$ans1 = $_POST['ans'][0];
$ans2 = $_POST['ans'][1];
$ans3 = $_POST['ans'][2];
$ans4 = $_POST['ans'][3];

Explanation

You only need to post ans[] repeatedly , and not

ans1[], ans2[], ans3[]... in order to get an array like

$_POST['ans'][0], $_POST['ans'][1]...

or you can use

ans1, ans2, ans3

(without brackets []) to read as

$_POST['ans1'], $_POST['ans2'], $_POST['ans3']...

Community
  • 1
  • 1
lilezek
  • 6,976
  • 1
  • 27
  • 45
  • He needs many of ans1, ans2, ans3 and ans4 though. One for every question. Naming them ans[] would create four for every questions where as his current way there would be 1 ans1, ans2, ans3 and ans4 per question. – Christian Oct 05 '15 at 15:35
0

When you save you should be running through a loop again. Try this maybe?

<?php

    if(isset($_POST['save'])){
        $user_id = $_SESSION['id'];
        require_once('xcon.php');
        foreach ($_POST['questions'] as $key => $question){
            $ans1 = $_POST['ans1'][$key];
            $ans2 = $_POST['ans2'][$key];
            $ans3 = $_POST['ans3'][$key];
            $ans4 = $_POST['ans4'][$key];
            $cans = $_POST['cans'][$key];


            echo "<input type = 'hidden' value = '$question'>";

            $query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans) 
                  VALUES ('NULL','$user_id','$question','$ans1','$ans2','$ans3','$ans4','$cans')";
            $result = mysql_query($query);

            if($result){
                echo 'Insert Success!<br>';
            }else{
                echo 'Error<br>';
            }
        }    
    }

?>
Christian
  • 1,557
  • 11
  • 16
0

You are using element naming in a strange way, you are using array, but still use numbers. Try generating like this:

for ($x = 0; $x <= $MC; $x++) {
    echo "<input type = 'text' name = 'questions[$i]'>";
    echo "A. <input type = 'text' name = 'ans[$i][A]'>";
    echo "B. <input type = 'text' name = 'ans[$i][B]'><br>";
    echo "C. <input type = 'text' name = 'ans[$i][C]'>";
    echo "D. <input type = 'text' name = 'ans[$i][D]'><br>";
    echo "Correct Answer: <input type = 'text' name ='cans[$i]'><br><br>";
}

Then you'll get the following results in your $_POST:

[
    "questions" => [
        0 => "question1",
        ...
    ]
    "ans" => [
        0 => [
            "A" => "answer A",
            "B" => "answer B",
            "C" => "answer C",
            "D" => "answer D",
        ]
        ...
    ]
    "cans" => [
        0 => "A",
        ....
    ]
]

Which is easily processed with a foreach:

foreach ($_POST['questions'] as $key => $question) {
    // $question == 'question1';
    $answers = $_POST['ans'][$key]; // array of answers
    $solution = $_POST['cans'][$key];
}
NDM
  • 6,731
  • 3
  • 39
  • 52