Xtremer360 Posted June 21, 2011 Share Posted June 21, 2011 Last question for the day. I'm trying to figure out how i can fix this becasue the post parameters of the form are: answer1[2], and answer2[4]. The number inside the brackets represent the id of the question it belongs to. Reason I need to know how to do this is because its not returning an error for the answers when its an empty form submission. <?php if((empty($_POST['answer1'])) || (trim($_POST['answer1'])=="") || ($_POST['answer1'] == NULL) || (!isset($_POST['answer1']))){$errors = "yes";} if((empty($_POST['answer2'])) || (trim($_POST['answer2'])=="") || ($_POST['answer2'] == NULL) || (!isset($_POST['answer2']))){$errors = "yes";} // Error checking, make sure all form fields have input if ($errors == "yes") { // Not all fields were entered error $message = "You must enter values to all of the form fields!"; $output = array('errorsExist' => true, 'message' => $message); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/ Share on other sites More sharing options...
requinix Posted June 21, 2011 Share Posted June 21, 2011 Try making your fields as name=answer[question id] and value=answer Question #2 Answer #1 Then use a loop on the answers to check that each question has an answer submitted. Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/#findComment-1233010 Share on other sites More sharing options...
Xtremer360 Posted June 21, 2011 Author Share Posted June 21, 2011 Here's what I have for my form code. <?php $i = 1; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { ?> <dl> <dt style="width: 190px;"><label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?></label></dt> <dd><input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" class="answers[]" size="54" /></dd> </dl> <?php ++$i; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/#findComment-1233012 Share on other sites More sharing options...
Xtremer360 Posted June 21, 2011 Author Share Posted June 21, 2011 okay so I have the above fixed but what I'm trying to do is run a select statement for each answer to select the answer in the datebase where the questionID = $i and the userID = $userID so I have the query like this set up so far but not sure what I'm missing or am I right and not missing anything? Also no matter what i do both fields have values but i'm still getting the error message that I need to fill out both form fields. <?php if (empty($_POST['answer1'][$i]) || trim($_POST['answer1'][$i])=="") {$errors = "yes";} if (empty($_POST['answer2'][$i]) || trim($_POST['answer2'][$i])=="") {$errors = "yes";} // Error checking, make sure all form fields have input if ($errors == "yes") { // Not all fields were entered error $message = "You must enter values to all of the form fields!"; $output = array('errorsExist' => true, 'message' => $message); } else { $userID = mysqli_real_escape_string($dbc,$_POST['userID']); $answer1 = mysqli_real_escape_string($dbc,$_POST['answer1'][$i]); $answer2 = mysqli_real_escape_string($dbc,$_POST['answer2'][$i]); $query = "SELECT * FROM manager_users_secretAnswers WHERE questionID = '".$questionID."' AND userID = '".$userID."'"; $result = mysqli_query($dbc,$query); echo $query; ?> Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/#findComment-1233034 Share on other sites More sharing options...
The Little Guy Posted June 21, 2011 Share Posted June 21, 2011 You could do this: <?php $is_answer1 = (bool)trim($_POST['answer1']); $is_answer2 = (bool)trim($_POST['answer2']); $errors = 0; if(!$is_answer1) $errors++; if(!$is_answer2) $errors++; if($errors > 0){ echo "You must enter values to all of the form fields!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/#findComment-1233046 Share on other sites More sharing options...
Xtremer360 Posted June 22, 2011 Author Share Posted June 22, 2011 How would I deal with the other part of the question? Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/#findComment-1233421 Share on other sites More sharing options...
Xtremer360 Posted June 22, 2011 Author Share Posted June 22, 2011 I changed up my code and have this now BUT when I put values for both form fields then it still gives me the error that I need to fill out all form fields and don't know why. http://pastebin.com/BwV5P3aw Quote Link to comment https://forums.phpfreaks.com/topic/240034-not-returning-error-message/#findComment-1233472 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.