Jump to content

Validating user input


Xtremer360

Recommended Posts

I'm trying to figure out what would be the best way to deal with this on the back end.  When I submit my form I want to verify that there were selections made to both questions and the answers but still be able to tie in the right answers to the right questions.

 

<?php 
<fieldset>
                        
                            <legend>Secret Questions</legend>
                        
                            <dl>
                            
                                <dt><label for="question1">Question #1:</label></dt>
                                <dd>
                                    <select size="1" name="questions[]" id="question1">
                                        <option value="">- Select -</option>
                                        <?php
                                        while ( $row = mysqli_fetch_array ( $result, MYSQLI_ASSOC ) ) { 
                                            print "<option value=\"".$row['id']."\">".$row['question']."</option>\r";
                                        }
                                        ?>
                                     </select>
                                </dd>
                                     
                            </dl>
                            
                            <dl>
                                
                                <dt><label for="answer">Answer:</label></dt> 
                                <dd><input type="text" name="answers[]" id="answer" size="54" /></dd>
                                
                            </dl>
                            
                            <dl>
                            
                                <dt><label for="question2">Question #2:</label></dt>
                                <dd>
                                    <select size="1" name="questions[]" id="question2">
                                        <option value="">- Select -</option>
                                        <?php
                                        mysqli_data_seek( $result, 0 );
                                        while ( $row = mysqli_fetch_array ( $result, MYSQLI_ASSOC ) ) { 
                                            print "<option value=\"".$row['id']."\">".$row['question']."</option>\r";
                                        }
                                        ?>
                                     </select>
                                </dd>
                                     
                            </dl>
                            
                            <dl>
                                
                                <dt><label for="answer">Answer:</label></dt> 
                                <dd><input type="text" name="answers[]" id="answer" size="54" /></dd>
                                
                            </dl>
                                   
                            <dl class="submit">
                            
                                <input type="submit" name="submit" id="submit" value="Enter" />
                            
                            </dl>
                        
                        </fieldset>
?>

Link to comment
https://forums.phpfreaks.com/topic/239604-validating-user-input/
Share on other sites

I'm trying to do a few things. I'm trying to verify that there was a selection made for  both question select boxes and that the user gave input into the answer text boxes. Then I'm trying to I guess form some sort of array so that the answer one ties into the id of the question that it goes with because in my answers db table i have

 

userID, questionID, answer

I just threw this together real fast, maybe it will help you figure something out

 

$correct_answers = array('green','blue','red');

$answers = $_POST['answers']; // as array

$empty = array();
foreach($answers as $answer)
{
if (empty($answer)) {
	$empty[$answer] = $answer;
}
}

if (!empty($empty)) {
// not empty, continue	
$i = 0;
$wrong = array();
foreach($answers as $answer)
{
	if ($answer != $correct_answers[$i])
		$wrong[$answer] = $answer;
	}

	$i++;
}

if (empty($wrong)) {
	// passed
} else {
	echo 'Wrong answers: <br />';
	foreach($wrong as $w)
	{
		echo $w.'<br />';
	}
}
} else {
echo 'Empty answers: <br />';
foreach($empty as $e)
{
	echo $e.'<br />';
}
}

You have 4 static elements correct? Select box "value" is the ID of the question? then the text input next to it is the answer right? Why not cause I know your likely pushing this through with a post/get via jquery match the pairs of data up.. you are the one that makes your string to get posted..

 

myfile.php&q1=ValueFromSelect1&a1=AnswerFromInput1&q2=ValueFromSelect2&a2=AnswerFromInput2 then in your backend. If any one of the 4 variables are empty or otherwise invalid to you reject it with an error. Otherwise you know variable q1 goes with a1 and q2 goes with... a2

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.