Jump to content

Not returning error message


Xtremer360

Recommended Posts

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);

}
?>

Link to comment
https://forums.phpfreaks.com/topic/240034-not-returning-error-message/
Share on other sites

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;
                            }
                            ?>

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;
?>

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!";
}
?>

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.