This one is bound to be simple (I hope) something I am overlooking. I have a set of 50 questions that are asked pulled from a table and put into a form for answering. I want to check to make sure they have all been answered (required). When the user hits submit, none of the alert boxes (even the debugging boxes) appear. What am I dong wrong here?
First, the PHP:
echo 'Please complete ALL 50 questions, then press the "SUBMIT" button at the bottom of the page.';
$query = "SELECT *
FROM `starriskquestions`
ORDER BY `QuestionNum` ASC";
$results = $pdo->query($query);
echo '<form name="submitra" action="riskassessmenttest.php" onsubmit="return validateForm()" method="post">';
while ($row = $results->fetch()) {
echo '<br>' .$row["QuestionNum"] . ') ' . $row["Question"] . '<br>
<input type="radio" name="a'.$row["QuestionNum"].'" value="1" /> Yes ----
<input type="radio" name="a'.$row["QuestionNum"].'" value="-1" /> No<br><br>';
}
echo "<br> ARE YOU SURE YOU ANSWERED ALL 50 QUESTIONS??? <br> If so, click the ";
echo "submit buton below <br>";
echo '<input type="hidden" name="testid" value="'.$testid.'">';
echo '<input type="submit" name="submittestanswers" value="submit">';
echo ' </form>';
Then the Javascript
function validateForm()
{
for (var answerloop=1; <=50; answerloop++)
{
var answernum = '"'+ "a" + answerloop + '"';
alert (answerloop);
var x=document.getElementByName(answernum).value;
alert ("This is the variable X: " + x);
if (x!=="1" || x!=="-1")
{
alert(" One or more questions must be filled out");
return false;
}
}
}