Jump to content

validating radio button helpp!!


furrls

Recommended Posts

<?php

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.* FROM stu_satisfaction_tblquestions LEFT JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id WHERE stu_satisfaction_tblquestions.answers_id BETWEEN '1' AND '2'
";
  
$result = mysql_query($SQL);
while ($data = mysql_fetch_array($result)) {

echo $data['question'].'<br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$data['answer1'].'" value="1" /><label for="'.$data['question_id'].'_'.$data['answer1'].'">'.$data['answer1'].'</label><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$data['answer2'].'" value="2" /><label for="'.$data['question_id'].'_'.$data['answer2'].'">'.$data['answer2'].'</label><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$data['answer3'].'" value="3" /><label for="'.$data['question_id'].'_'.$data['answer3'].'">'.$data['answer3'].'</label><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$data['answer4'].'" value="4" /><label for="'.$data['question_id'].'_'.$data['answer4'].'">'.$data['answer4'].'</label><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$data['answer5'].'" value="5" /><label for="'.$data['question_id'].'_'.$data['answer5'].'">'.$data['answer5'].'</label><br/>';
}
?>

 

 

this is my form, how should i validate the radio buttons of it?

Link to comment
Share on other sites

yea, but i'm printing many question from a while loop so there will some question that the user will miss and he will just submit without any validation, so how could prevent that? such as user will have to comeplete ALL question before he is able to submit

Link to comment
Share on other sites

I believe that if the user does not provide an answer for one of the groups, that group will not post at all. So you can count the number of answers you get and compare it to the number of questions you sent.

// Untested sample code
if (count($_POST['answers'])) < $numberOfQuestions) { 
  // NOT ALL OF THE QUESTIONS WERE ANSWERED
} else {
  foreach ($_POST['answers'] as $questionID => $answerValue) {
    // RECORD THE answerValue FOR THE questionID
  }
}

Link to comment
Share on other sites

ok when i submit the form, it still insert the result despite blank answers at my process page.

 

 

this is my code at the process page

 

$studID = $_POST['studentID'];

		if ((count($_POST['answers'])) < (count($_POST['question']))) { 
  die("PLEASE COMPLETE ALL QUESIOJN");
} else {

		foreach ($_POST['answers'] as $qID=>$answer) {
		echo ''.$_SESSION['username'].' answered '.$answer.' for qID '.$qID.'<br/>';
	    $SQL = "INSERT INTO responses (student_id, question_id, answer) VALUES ('$studID','$qID', '$answer')";
		$result = mysql_query($SQL);
		}
		}

print "Thanks for voting!";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.