Make sure all questions have an answer

I’ve got a dynamically generated “poll” type form. Before a user can submit, I need to be sure:

  1. They’ve not already submitted the form previously
  2. Every question has an answer.

So first, my form code (simplified somewhat for posting here)


<div class="question-box" id="question-125" rel="chk">
   <h2>Questoion 1</h2>
      <input type="radio" id="answer-303" name="ans-125" value="125|303" />
      <input type="radio" id="answer-304" name="ans-125" value="125|304" />
      <input type="radio" id="answer-305" name="ans-125" value="125|305" />
</div>

<div class="question-box" id="question-128" rel="chk">
   <h2>Please pick two answers from the below </h2>
      <input type="checkbox" id="answer-311" name="ans-128_1" onclick="boxchk(this,2)" value="128|311" />
      <input type="checkbox" id="answer-312" name="ans-128_2" onclick="boxchk(this,2)" value="128|312" />
      <input type="checkbox" id="answer-313" name="ans-128_3" onclick="boxchk(this,2)" value="128|313" />
      <input type="checkbox" id="answer-314" name="ans-128_4" onclick="boxchk(this,2)" value="128|314" />
</div>

Each question is contained in a “question-box” div with the possible answers for that question inside the div.

My AJAX part is working perfect. It checks the database, and if the user-id is found in the “survey_completed” table, it displays the alert and disables the submit button.

I can’t get the validation part to work though. For every “question-box” id, there’s got to be at least one matching answer (some allow for more than one).

Been staring at this for awhile. Firefox console isn’t showing any obvious errors, but there’s still something wrong somewhere. The form will cheerfully submit and record NULL answers for any skipped questions.

What seems my goof at the moment?


<script type="text/javascript">
$(document).ready(function(){
	$("#submit-ballot-button").bind("click", function(){
		$.ajax({type: "POST", url: "/members/ajax_requests/validate_submission.php", data: "ballot_id="+$("#ballot-id").val()+"&user_id=<?=$_SESSION["surv_user_id"]?>",
			success: function(submission_check){
				var errors = false;
				var answer_array = new Array($(".question-box").size());
				for(var i = 0; i < $(".question-box").size(); i++)
				  {
					answer_array[i]=new Array(2)
				  }
				
				if(submission_check == "false")
				{
				        // VALIDATION TO MAKE SURE EVERYTHING IS CHECKED
					    $(".question-box").each(function()
					    {
					       var question = $(this).attr("id").substr(9);
					       var rel = $(this).attr("rel");
					       var answer = "";
					       if(rel == "chk")
					          {
							      $("#"+$(this).attr("id")+" input:checked").each(function(){
								  answer = $(this).attr("id").substr(7);
								  });
								
                                  if(answer == "") { errors = true; }
								   if(errors)
					                  {
						                 $("#submission-error-text").html("Please respond to all answers before submitting.");
						                 $("#submission-error").show();
					                  }
					          }
			
					});

				   // END VALIDATION
				}
				else
				{
					$("#submission-error-text").html("You have already submitted your response.");
					$("#submission-error").show();
				}
			}});
	});
});
</script>

Finally got it to work. My coffee machine is broke and I have no hair left though …


function boxchk(obj,max) {
	 var box = obj.name.substr(0,obj.name.lastIndexOf('_')+1);
	 var cnt=0,i=1;
	  while(obj.form[box+i]) {
		   cnt += obj.form[box+i].checked;i++;}
     if (cnt > max) {
		   obj.checked = false;
		   alert('You can only choose ' + max + ' checkboxes.\
To pick this option unselect one of the others.');}
  }
  
  
// VALIDATION TO MAKE SURE EVERYTHING IS CHECKED
function checkALL() 
{
	var errors = false;	
	var j = 0;
	var answer_array = new Array($(".question-box").size());
	for(var i = 0; i < $(".question-box").size(); i++)
	{
		answer_array[i]=new Array(2)
	}
	
	$(".question-box").each(function() 
	{
		var answer = "";
		var question = $(this).attr("id").substr(9);
		var rel = $(this).attr("rel");		
		if (rel == "chk")
		{
			$("#" + $(this).attr("id") + " input:checked").each(function() 
			{
				answer = $(this).attr("id").substr(7);
			});
		}
		else
		{
			answer = $('#answer-'+question).val();	
		}
		
		if(answer == "")
		{
			errors = true;
		}
		else
		{
			answer_array[j][0] = question;
			answer_array[j][1] = answer;
			j++;
		}	
	});
	
	if (errors) 
	{
		alert("Please respond to all answers before submitting");
		return false;
	}
	else
	{
		return true;
	}
	
}