Xtremer360 Posted June 17, 2011 Share Posted June 17, 2011 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> ?> Quote Link to comment https://forums.phpfreaks.com/topic/239604-validating-user-input/ Share on other sites More sharing options...
redixx Posted June 17, 2011 Share Posted June 17, 2011 I'm not really understanding what you're trying to do, can you try to clarify? Quote Link to comment https://forums.phpfreaks.com/topic/239604-validating-user-input/#findComment-1230860 Share on other sites More sharing options...
Xtremer360 Posted June 17, 2011 Author Share Posted June 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239604-validating-user-input/#findComment-1230862 Share on other sites More sharing options...
redixx Posted June 17, 2011 Share Posted June 17, 2011 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 />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/239604-validating-user-input/#findComment-1230868 Share on other sites More sharing options...
Xtremer360 Posted June 17, 2011 Author Share Posted June 17, 2011 I tried working with it but couldn't get anything with it, but it feels as if this could be done differently. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/239604-validating-user-input/#findComment-1230892 Share on other sites More sharing options...
monkeytooth Posted June 17, 2011 Share Posted June 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239604-validating-user-input/#findComment-1231028 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.