furrls Posted December 17, 2010 Share Posted December 17, 2010 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/ Share on other sites More sharing options...
Maq Posted December 17, 2010 Share Posted December 17, 2010 Sorry, but what do you mean validate? Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/#findComment-1148715 Share on other sites More sharing options...
furrls Posted December 17, 2010 Author Share Posted December 17, 2010 validate as in u have to fill in all the radio buttons before you can submit if not a popout or any error will show it to the user Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/#findComment-1148731 Share on other sites More sharing options...
Maq Posted December 17, 2010 Share Posted December 17, 2010 validate as in u have to fill in all the radio buttons before you can submit if not a popout or any error will show it to the user By the nature of a radio button group, you can only select one. Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/#findComment-1148733 Share on other sites More sharing options...
furrls Posted December 17, 2010 Author Share Posted December 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/#findComment-1148735 Share on other sites More sharing options...
DavidAM Posted December 17, 2010 Share Posted December 17, 2010 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/#findComment-1148740 Share on other sites More sharing options...
furrls Posted December 17, 2010 Author Share Posted December 17, 2010 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!"; Quote Link to comment https://forums.phpfreaks.com/topic/221993-validating-radio-button-helpp/#findComment-1148745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.