Empty Radio Buttons messing up Posted Form

Ugh! I am stuck in the most involved script I have probably ever written and in great pain at the moment… :frowning: :frowning: :frowning:

Like most things I do, it is spread across a couple of scripts and I can’t just post my entire code-base here?!

Let me try my best to explain what is going on…

One ARTICLE can have many SURVEY QUESTIONS
One SURVEY QUESTION can be associated with many Articles
They are matched together in my SURVEY MAP.

Instead of hard-coding Survey Questions for an Article, I designed a script which lets me define a “Survey Map” and then I build each Survey dynamically using a function generateSurveyItem() which creates HTML for every question and then I display each Dynamic Question in a ForEach loop.

Everything has been working great, until I got to the part where I handle my Form, and specifically, I try to display a Validation Error for Text Fields that have too long of a response.

And here is the issue…

When I redisplay my Form with Validation Errors, I was just trying to use the $_POST array like this…


	// Retrieve Form Data.
	foreach($_POST['responseToQuestion'] as $articleSurveyQuestionID => $surveyResponse){

			// ************************
			// Validate Form Data.		*
			// ************************

			// Call Validation Function.
			list($responseOK, $errorMsg) = validateSurveyResponse($dbc, $articleSurveyQuestionID, $surveyResponse);

			echo "<p>\\$responseOK = $responseOK, <br />\\$errorMsg = $errorMsg</p>";

			// Build Array.
			$surveyItemArray[$articleSurveyQuestionID] = generateSurveyItem($dbc, $questionNo, $articleSurveyQuestionID, $surveyResponse, $errorMsg);

But I just discovered that if a User didn’t answer Questions that involve Radio Buttons, that they never show up in the $_POST array?! :eek:

So, if I had 5 Questions…
1.) True-False
2.) True-False
3.) Open-Ended
4.) Likert
5.) Open-Ended

After the Form is re-displayed with Validation Errors, I would get this…
1.) Open-Ended
2.) Open-Ended

I guess I could use my l-o-n-g set of code that calls the database, but it would be easier if regardless of what is or is not answered in the Survey, that I get ALL QUESTIONS returned back in the $_POST.

Any suggestions??

Thanks,

Debbie

This sounds like one of those places where Javascript validation (on the client) is most appropriate.
Check all the form fields, validate proper entries, and either

  • notify the user they missed something (the form is incomplete)
  • submit the form content (and your PHP script will know all values exist)

Except everything should be checked on the Server too, plus I don’t use JavaScript at this point.

Think I have a solution after going to supper, but this script sure is a bear!!!

Debbie