Jump to content

Validation mixup?


mikebrowntsbod

Recommended Posts

 

I am selecting the public option in the dropdown and I am finding out that when I do my validation as below it is printing out that I picked an invalid session. What am I missing?

<?php require "../login/loginheader.php"; ?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <title>Board Add E-Motion</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <link href="../css/bootstrap.css" rel="stylesheet" media="screen">
      <link href="../css/main.css" rel="stylesheet" media="screen">
      <link rel="stylesheet" href="../css/bootstrap337.css">
      <link href="https://fonts.googleapis.com/css?family=Oswald:700|Patua+One|Roboto+Condensed:700" rel="stylesheet">
      <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
      <style>
         h1,h2,h3 {
         margin:0 0 35px 0;
         font-family: 'Patua One', cursive;
         text-transform: uppercase;
         letter-spacing:1px;
         }
         p{
         margin:0 0 25px;
         font-size:18px;
         line-height:1.6em;
         }
         a{
         color:#26a5d3;
         }
         a:hover,a:focus{
         text-decoration:none;
         color:#26a5d3;
         }
         #contact{
         background:#333333;
         color:#f4f4f4;
         padding-bottom:80px;
         }
         textarea.form-control{
         height:100px;
         }
         .others {
         color:black;
         }
         .black-text form select input
         {
         color:black;
         }
      </style>
   </head>
   <body>
      <?php include "../includes/rolecheck.php"; ?>
      <?php include "../includes/menu.php"; ?>
      <p class="navbar-brand"><strong>Add Motion</strong></p>
      <?php include "../includes/lowermenu.php"; ?>
      <!--Added Here-->
      <?php
         if ($_SERVER['REQUEST_METHOD'] != 'POST')
         {
           echo'      <section id="contact" class="content-section text-center">
         <div class="contact-section">
            <div class="container">
               <h2>Add a Board Motion</h2>
               <p>Use this form to submit an E-Motion for the Board. Remember all E-motions must be anonymously approved otherwise
                  it will be referred to the next Public Board Meeting or next Executive Session
               </p>
               <div class="row">
                  <div class="col-md-8 col-md-offset-2 black-text">
                     <form class="form-horizontal" method="post">
                        <div class="form-group">
                           <label for="Motion_Name">Motion Name</label>
                           <input type="text" class="form-control" name="Motion_Name" id="Motion_Name" placeholder="Motion Name">
                        </div>
                        <div class="form-group black-text">
                           <label for="boardsession">Session</label>
                           <select name="boardsession" id="boardsession">
                              <option value="" class="others"></option>
                              <option value="Executive" class="others">Executive</option>
                              <option value="Public" class="others">Public</option>
                           </select>
                        </div>
                        <div class="form-group black-text">
                           <label for="motionText">Motion Text</label>
                           <textarea  class="form-control" id="motionText" name="motionText" placeholder="Description"></textarea> 
                        </div>
                        <!--<button type="submit" class="btn btn-default">Send Message</button>-->
                          <input type="submit" value="Send Message" class="black-text"/>
                     </form>
                     <hr>
                  </div>
               </div>
            </div>
         </div>
         </section>';
            
         }
         
         else
         
         {
            $motionName=$_POST['Motion_Name'];
            $session=$_POST['boardsession'];
            $motionText=$_POST['motionText'];
            $errorMessage="";
            
            if ($motionName == "")
            {
               $errorMessage.="Your motion name was blank and should not be blank<br />";
            }
            elseif ($motionText == "")
            {
               $errorMessage.="Your motion text was blank<br />";
            }
            elseif ($_POST['boardsession'] != 'Public' || $_POST['boardsession'] != 'Executive')
            {
               $errorMessage.="You choosed a session that was not valid<br />";
               echo "Session: $session";
            }
            
            if ($errorMessage == "")
            {
               //insert
            }
            else
            {
                echo '<div class="alert alert-danger">';
               echo "$errorMessage";
               echo "</div>";
            }
               
                 
            
         }
         
         ?>
   </body>
</html>
Link to comment
Share on other sites

Your code is all kinds of messed up. All the Php should be at the top and the HTML below. The server request check should be where you are executing your php code, not dumping a bunch of Html to the screen. You should be collecting ALL the errors in an array and then displaying them all if any. You are incorrectly using the word session. Experienced programmers know that word for something completely different than an unnecessary variable name for a dropdown. You are also creating variables for nothing. Stop doing that. Put your CSS in an external file.

 

If you still have a problem after making these changes then post your updated code and we will help you.

<?php
//Php Here
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//Process form
} 
?>
HTML Here
Link to comment
Share on other sites

Your code is all kinds of messed up. All the Php should be at the top and the HTML below. The server request check should be where you are executing your php code, not dumping a bunch of Html to the screen. You should be collecting ALL the errors in an array and then displaying them all if any. You are incorrectly using the word session. Experienced programmers know that word for something completely different than an unnecessary variable name for a dropdown. You are also creating variables for nothing. Stop doing that. Put your CSS in an external file.

 

If you still have a problem after making these changes then post your updated code and we will help you.

<?php
//Php Here
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//Process form
} 
?>
HTML Here

 

 

I believe I have fixed the issues you identified minus collecting the errors in an array. This is a rough model that I am showing. I will do all of this in the future with what you are talking about. I am still working on that. I am still getting this error

 

You choose a session that was not valid

 

<?php require "../login/loginheader.php"; ?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <title>Board Add E-Motion</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <link href="../css/bootstrap.css" rel="stylesheet" media="screen">
      <link href="../css/main.css" rel="stylesheet" media="screen">
      <link href="../css/extra.css" rel="stylesheet" media="screen">
      <link rel="stylesheet" href="../css/bootstrap337.css">
      <link href="https://fonts.googleapis.com/css?family=Oswald:700|Patua+One|Roboto+Condensed:700" rel="stylesheet">
      <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
   </head>
   <body>
      <?php include "../includes/rolecheck.php"; ?>
      <?php include "../includes/menu.php"; ?>
      <p class="navbar-brand"><strong>Add Motion</strong></p>
      <?php include "../includes/lowermenu.php"; ?>
      <!--Added Here-->
      <?php
         if ($_SERVER['REQUEST_METHOD'] == 'POST')
         {
             $motionName=$_POST['Motion_Name'];
            $session=$_POST['boardsession'];
            $motionText=$_POST['motionText'];
            $errorMessage="";
            
            if ($motionName == "")
            {
               $errorMessage.="Your motion name was blank and should not be blank<br />";
            }
            elseif ($motionText == "")
            {
               $errorMessage.="Your motion text was blank<br />";
            }
            elseif ($_POST['boardsession'] != 'Public' || $_POST['boardsession'] != 'Executive')
            {
               $errorMessage.="You choose a session that was not valid<br />";
               echo "Session: $session";
            }
            
            if ($errorMessage == "")
            {
               //insert
            }
            else
            {
                echo '<div class="alert alert-danger">';
               echo "$errorMessage";
               echo "</div>";
            }
               
             
         }
         
         else
         
         {
           echo'      <section id="contact" class="content-section text-center">
         <div class="contact-section">
            <div class="container">
               <h2>Add a Board Motion</h2>
               <p>Use this form to submit an E-Motion for the Board. Remember all E-motions must be anonymously approved otherwise
                  it will be referred to the next Public Board Meeting or next Executive Session
               </p>
               <div class="row">
                  <div class="col-md-8 col-md-offset-2 black-text">
                     <form class="form-horizontal" method="post">
                        <div class="form-group">
                           <label for="Motion_Name">Motion Name</label>
                           <input type="text" class="form-control" name="Motion_Name" id="Motion_Name" placeholder="Motion Name">
                        </div>
                        <div class="form-group black-text">
                           <label for="boardsession">Session</label>
                           <select name="boardsession" id="boardsession">
                              <option value="" class="others"></option>
                              <option value="Executive" class="others">Executive</option>
                              <option value="Public" class="others">Public</option>
                           </select>
                        </div>
                        <div class="form-group black-text">
                           <label for="motionText">Motion Text</label>
                           <textarea  class="form-control" id="motionText" name="motionText" placeholder="Description"></textarea> 
                        </div>
                        <!--<button type="submit" class="btn btn-default">Send Message</button>-->
                          <input type="submit" value="Send Message" class="black-text"/>
                     </form>
                     <hr>
                  </div>
               </div>
            </div>
         </div>
         </section>';   
         }
         
         ?>
   </body>
</html>

Link to comment
Share on other sites

Ok my friend, you really need to RTFM and learn about die and exit. You will also need to learn about if and empty. Take a close look at your double negative and see if you can't make sense of why that cannot work.

 

* Hint if evaluates if something is true or false.

 

As described in the section about expressionsexpression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it.

Link to comment
Share on other sites

The logic is messed up here:

 

elseif ($_POST['boardsession'] != 'Public' || $_POST['boardsession'] != 'Executive')

 

When the value is 'Public' this condition ends up as ( False || True ) and hence evaluates to TRUE. You end up with (True||False) when it's 'Executive' and still evals to TRUE.

Link to comment
Share on other sites

Your select/option list is a SET of data. Arrays are for sets. You need to define the list of choices in an array. Once you do this, you can -

 

1) Dynamically produce the <option...>...</option> markup. Just loop over the array and output the markup for each choice inside the loop.

 

2) By doing item #1, you can easily pre-select the option that has previously been picked, so that the user doesn't need to keep selecting things when there are validation errors.

 

3) Use this same defining array in your validation logic, If the submitted value is in the array, it's a valid choice. If it's not in the array, it's not valid. See php's in_array().

 

4) Make any changes to the list of choices (for this or a completely different application) by simply changing the defining array. The logic producing the option tags and the validation logic will remain the same.

Link to comment
Share on other sites

Your code does have a lot of problems (as benanamen alluded to), but I'm not going to go into all the problems that exist. It would take up too much of my time.

 

But, to answer you specific question, the problem is here

 

elseif ($_POST['boardsession'] != 'Public' || $_POST['boardsession'] != 'Executive')
{
  // Error condtion

 

Think about that logic for a minute: If the value does not equal 'Public' OR if the value does not equal 'Executive'.

 

You have an OR statement, so if either condition is TRUE, the result will be true. It is impossible for that condition to ever be FALSE, because any value will make one of those condition TRUE. You should be using an AND condition.

Link to comment
Share on other sites

 

Goodness no! What if the dropdown had 25 options. You're going to do 25 AND conditions? It is as simple as checking for the ONE empty condition.

if (empty($_POST['boardsession'])){
//Error
}

 

Are you assuming that the user can only pass valid values because it is a select list? That is a very bad assumption. It is very simply to override the values provided in a select list. He wants to validate that the user has selected a valid value - which is a legitimate use case. For only two possible values, I think a simple AND condition is fine. However, if it goes beyond that, I would suggest using an array of valid value and using the in_array() function instead.

Link to comment
Share on other sites

Please show me how you would use AND for the two values. AND would require both values to be selected. OP is not using a Multi-Select which means he is only expecting one of the values.

 

The last part using in_array is suitable though if you want to make sure invalid values are not attempting to be submitted by some other means.

Link to comment
Share on other sites

Oh, I was just going for the joke, mostly. I'd use in_array() with a set list.

 

Unless I'm missing something, though.

 

<?php

$value = 'Foo';

if($value != 'Public' && $value != 'Executive') {
    echo 'Invalid value!';
} else {
    echo 'All good';
}

?>
Link to comment
Share on other sites

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.