0

I have a webpage I am making that will enable a user to create a series of questions for an online quiz. The page is working for the most part except I have one issue I can not figure out.

The issue is that when I click save the questions and answers are saved correctly but their is a checkbox that is used to indicate if the answer is the correct answer or not and that is not being saved correctly. What is happening is if I have three questions the it saves the first three potential answers for the first question all have the "yes" value saved in their row and I am not sure why.

Below is my PHP code to grab the info from the form and insert into a mysql via php

//if save was clicked
if(isset($_POST['saveit']))
{
    $tmodnameid = $_POST['tmodnameid'];
    $tmodnameid = mysqli_real_escape_string($dbc, $tmodnameid);

    mysqli_query($dbc, "INSERT into training_quizs(tmoduleid, quiz_createdon, quiz_created_by) VALUES('$tmodnameid','$timedate_rightnowis','$myuid')");
    $quizid = mysqli_insert_id($dbc);

    //lets save all the questions and answers. Each question can potentially have 6 answers
    $n = 0;
    $newquestion = $_POST['newquestion'];
    foreach($newquestion as $aquestion)
    { 

        mysqli_query($dbc, "INSERT into training_quizs_questions(quizid, qquestion) VALUES('$quizid','$aquestion')");
        $questionrowid = mysqli_insert_id($dbc);

        //lets save each of this questions answers

        //save first potential answer for this question
        $answera = $_POST['answera'][$n];
        $isanswera = $_POST['isanswera'][$n];
        mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) VALUES('$questionrowid','$answera','$isanswera')");

        //save second potential answer for this question
        $answerb = $_POST['answerb'][$n];
        $isanswerb = $_POST['isanswerb'][$n];
        mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) VALUES('$questionrowid','$answerb','$isanswerb')");

        //save third potential answer for this question
        $answerc = $_POST['answerc'][$n];
        $isanswerc = $_POST['isanswerc'][$n];
        mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) VALUES('$questionrowid','$answerc','$isanswerc')");

        //save fourth potential answer for this question
        $answerd = $_POST['answerd'][$n];
        $isanswerd = $_POST['isanswerd'][$n];
        mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) VALUES('$questionrowid','$answerd','$isanswerd')");

        //save fifth potential answer for this question
        $answere = $_POST['answere'][$n];
        $isanswere = $_POST['isanswere'][$n];
        mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) VALUES('$questionrowid','$answere','$isanswere')");

        //save sixth potential answer for this question
        $answerf = $_POST['answerf'][$n];
        $isanswerf = $_POST['isanswerf'][$n];
        mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer) VALUES('$questionrowid','$answerf','$isanswerf')");

        $n++;
    }

    //get rid of any blank rows that were created 
    mysqli_query($dbc, "DELETE from training_quizs_answers WHERE answer=''");
    echo "<div class='alert alert-success'>                
            <h4>Success!</h4>
            You successfully setup a new quiz from the training module.
        </div>";

    }

My HTML is below. I have a javascript that enables a user to click a button and dynamically add as many question/answer text boxes as needed.

<head>
          <script language="javascript">
fields = 2;
function addInput() {
if (fields != 50) {
document.getElementById('text').innerHTML += "<br /><br />" + fields +") Question<br />    <textarea name='newquestion[]' /></textarea><br />ANSWERS:<br /><div style='float:left;     padding-right:20px'> A) <input type='text' name='answera[]' size='90'/></div><div     style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswera[]'     value='yes'> is answer</div><br style='clear:both'><div style='float:left; padding-right:20px'> B) <input type='text' name='answerb[]' size='90'/></div><div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerb[]' value='yes'> is answer</div><br style='clear:both'/><div style='float:left; padding-right:20px'> C) <input type='text' name='answerc[]' size='90'/></div><div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerc[]' value='yes'> is answer</div> <br style='clear:both'/><div style='float:left; padding-right:20px'> D) <input type='text' name='answerd[]' size='90'/></div><div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerd[]' value='yes'> is answer</div><br style='clear:both'/><div style='float:left; padding-right:20px'> E) <input type='text' name='answere[]' size='90'/></div><div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswere[]' value='yes'> is answer</div><br style='clear:both'/><div style='float:left; padding-right:20px'> F) <input type='text' name='answerf[]' size='90'/></div><div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerf[]' value='yes'> is answer</div><br /><br style='clear:both'>";
fields += 1;
} else {
document.getElementById('text').innerHTML += "<br />Only 50 questions are able to be     created.";
document.form.add.disabled=true;
}
}
</script>
</head>
<body>
<form method='POST' action='' enctype='multipart/form-data' name='form1' id='form1'>
    <div class="row-form clearfix">
        <div class="span4">
            Training Module Name 
            <select name='tmodnameid'>
            <?php
                //lets select all training modules that have not quiz yet
                $tlistsql = mysqli_query($dbc, "SELECT tmoduleid, tmodule_name FROM training_modules WHERE tmodule_quizid='' ORDER by tmodule_name desc");
                while($tlistrow = mysqli_fetch_array($tlistsql))
                {
                    $tmoduleid = $tlistrow['tmoduleid'];
                    $tmodule_name = stripslashes($tlistrow['tmodule_name']);
                    echo "<option value='$tmoduleid'>$tmodule_name</option>";
                }
            ?>
            </select>
        </div> 
    </div> 

    <div class="row-form clearfix">
        <input type="button" onclick="addInput()" name="add" value="Add Question" />
        <br />
        <br />
        1) Question
        <br />
        <textarea name='newquestion[]' /></textarea>
        <br />
        ANSWERS:
        <br />
        <div style='float:left; padding-right:20px'> A) <input type='text' name='answera[]' size='90'/></div>
        <div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswera[]' value='yes'> is answer</div>
        <br style='clear:both'>

        <div style='float:left; padding-right:20px'> B) <input type='text' name='answerb[]' size='90'/></div>
        <div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerb[]' value='yes'> is answer</div>
        <br style='clear:both'/>

        <div style='float:left; padding-right:20px'> C) <input type='text' name='answerc[]' size='90'/></div>
        <div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerc[]' value='yes'> is answer</div>
        <br style='clear:both'/>

        <div style='float:left; padding-right:20px'> D) <input type='text' name='answerd[]' size='90'/></div>
        <div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerd[]' value='yes'> is answer</div>
        <br style='clear:both'/>

        <div style='float:left; padding-right:20px'> E) <input type='text' name='answere[]' size='90'/></div>
        <div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswere[]' value='yes'> is answer</div>
        <br style='clear:both'/>

        <div style='float:left; padding-right:20px'> F) <input type='text' name='answerf[]' size='90'/></div>
        <div style='float:left; padding-top:20px!important'> <input type='checkbox' name='isanswerf[]' value='yes'> is answer</div>
        <br /><br style='clear:both'>
        <div id='text'>

        </div>                          
    </div>

    <div class="footer tar">
        <input type='submit' name='saveit' value='Save' is='submit' class='btn'></form>
    </div>            
</body>
4
  • I think you meant "there is a checkbox", not "their is a checkbox". Commented Mar 19, 2014 at 17:20
  • 1
    Thanks for the grammar correction. Any code suggestions? ;) Commented Mar 19, 2014 at 17:24
  • 1
    Only checked boxes are submitted in a form. So $_POST['answera'][0] is not the checkbox from the first row, it's the first checkbox that's checked. Commented Mar 19, 2014 at 17:29
  • Ahh yes. So based on my code how might I identify which answer had a "yes" in its checkbox. each question has 6 potential answers and only one of the 6 check boxes will have a 'yes' value for any given set of questions Commented Mar 19, 2014 at 17:32

1 Answer 1

0

Unchecked boxes will post nothing, as if they did not exist. There's a lot that can be done to tighten up your code but I won't go all out here. A quick fix is to set explicit keys for your checkbox name values:

<input type='checkbox' name='isanswere[0]' value='yes'>
<input type='checkbox' name='isanswere[1]' value='yes'>
<input type='checkbox' name='isanswere[2]' value='yes'>
etc.

That way $_POST['isanswere'][$n]; will correspond to the checkbox you think. I would suggest doing the same for your answer fields as well.

2
  • So one more question. I tried doing $answera = $_POST['answera'][$n]; $answera = mysqli_real_escape_string($dbc, $answera); $isanswera = $_POST['isanswera'][$n]; $isanswera = mysqli_real_escape_string($dbc, $isanswera); How should I sanatize the $_post[] since the mysqli_real_escape_string() does not work because the posts are arrays? Commented Mar 19, 2014 at 17:48
  • I think you may need to check if that key exists or you'll get a reference error in php: $isanswere = array_key_exists($n, $_POST['isanswere']); Commented Mar 19, 2014 at 17:53

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.