Jump to content

[SOLVED] Form validation help!


gigantorTRON

Recommended Posts

Hey everyone....

I have a quiz application that pulls an answer and its related choices from database tables and outputs them to a page. There are 3 questions per page which can contain different form types (radio button and text box for example). Each question is assigned a unique  question id and each answer/choice is assigned a name of question_id and a value of choice (a, b, c, d, etc.).

 

Here's my issue... I need to perform validation on each page of the quiz. I would prefer to use php validation, but I dont know how to check each question individually since they are different types. For example, if one question contains a text box and check boxes, then I need to check that the text box is filled with something and that at least one check box is checked. Due to the naming differences between text boxes and radio buttons etc. the names are different. For example, a text box will have a name of 15a. 15 being the question# and a being the choice id (where it lies within a question). A check box will have a name = 15 and a value=a. When the user hits 'submit' it throws POST data to this script...

 

<?php
$id = $_SESSION["id"];
$question = $_GET['question'];
$pg = $_GET['page'];
$section = $_GET['section'];

$npg = $pg+1;

foreach ($_POST as $name=>$value) {

	//if name is text-related like 15a, 15b (this relates to text boxes and text areas exclusively)
	if (eregi("[a-z]", $name)) {
		$choice = trim($name, '0..9');
		$name = trim($name, 'a..z');
		if($pg!=7)
			$class->drop_answer_t($id, $name, $choice);
	for ($x=0; $x<count($value); $x++) {
		if($pg!=7)
			$class->save_answer($id, $name, $choice, $value[$x]);	

		//if the question answered is the last in a section, forward to section complete page
		if(!($class->check_section_end($question, $section))) {
				}
?>

 

Here's the save_answer function:

 

<?php
function save_answer($user_id, $qid, $cid, $text) {

	$query = "INSERT INTO progress_choices VALUES(" . $user_id . ", " . $qid . ", " . "'" . $text . "', " . "'" . $cid . "')";

	mysql_query($query) or die (mysql_error());

	return true;
}?>

 

Any help would be appreciated.... this may be pretty hairy, but let me know if I can post anything else to help. I'm just not sure where to put the error checking... all I can think of at the moment is running more queries on the db to get the question type and then running a certain checking function based on type.

 

Thanks.

 

Link to comment
Share on other sites

seriously though, your question is quite hard to understand...  what needs to be validated, the text box, no?  If you need one check box to be checked, I'd recommend using javascript instead, it's much more convenient for the user too.

 

<input type="checkbox" id="check" />

<script>
function check_boxes(){
if(document.getElementById(check).value == 'the_associated_value'){
one-box_is_checked = true;

 

 

Link to comment
Share on other sites

Yeah, i was afraid it might be confusing...

Basically I need to validate that all the questions have been answered before allowing the user to go to the next page... I suppose I could use javascript but would prefer php in case a user has disabled js in their browser.

 

yeah, it's something I hear techies talk about, but I've no idea why a user would do this.  If you disable javascript or don't install flash or accept cookies then you're pretty much opting out of anything fun at all, and I'd have said that includes quizzes, no?  I build sites for clients for proper money and I never give the 'disabled javascript' scenario a second thought... screw 'em ;)  I've never had a complaint anyway.

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.