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>
$_POST['answera'][0]
is not the checkbox from the first row, it's the first checkbox that's checked.