mikebrowntsbod Posted October 16, 2017 Share Posted October 16, 2017 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> Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/ Share on other sites More sharing options...
benanamen Posted October 16, 2017 Share Posted October 16, 2017 (edited) 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 Edited October 16, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552723 Share on other sites More sharing options...
mikebrowntsbod Posted October 16, 2017 Author Share Posted October 16, 2017 (edited) 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> Edited October 16, 2017 by mikebrowntsbod Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552724 Share on other sites More sharing options...
benanamen Posted October 16, 2017 Share Posted October 16, 2017 (edited) There was no point posting code again since you didn't do everything I told you. Your form should not be in the PHP either. Get rid of the else and stop echoing 100% HTML. I have given you specific things to do. Do all of it before you post code again. Edited October 16, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552725 Share on other sites More sharing options...
mikebrowntsbod Posted October 16, 2017 Author Share Posted October 16, 2017 (edited) The else is needed so the form does not show up after submission. I will apologize that I was using session in the incorrect way. I know that but I did it really quick and was running from notes. I know when to admit fault. Edited October 16, 2017 by mikebrowntsbod Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552726 Share on other sites More sharing options...
benanamen Posted October 16, 2017 Share Posted October 16, 2017 (edited) 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 expressions, expression 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. Edited October 16, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552727 Share on other sites More sharing options...
Sepodati Posted October 16, 2017 Share Posted October 16, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552732 Share on other sites More sharing options...
mac_gyver Posted October 16, 2017 Share Posted October 16, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552734 Share on other sites More sharing options...
Psycho Posted October 16, 2017 Share Posted October 16, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552744 Share on other sites More sharing options...
benanamen Posted October 16, 2017 Share Posted October 16, 2017 You should be using an AND condition. 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 } Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552745 Share on other sites More sharing options...
Sepodati Posted October 16, 2017 Share Posted October 16, 2017 there are only two conditions and that will NEVER change, though, right? right? we all know that... Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552747 Share on other sites More sharing options...
mikebrowntsbod Posted October 16, 2017 Author Share Posted October 16, 2017 there are only two conditions and that will NEVER change, though, right? right? we all know that... I have it worked out now. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552748 Share on other sites More sharing options...
Psycho Posted October 17, 2017 Share Posted October 17, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552783 Share on other sites More sharing options...
benanamen Posted October 17, 2017 Share Posted October 17, 2017 (edited) 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. Edited October 17, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552785 Share on other sites More sharing options...
Barand Posted October 17, 2017 Share Posted October 17, 2017 Boolean algebra 101 NOT (A OR B) <==> NOT A AND NOT B Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552786 Share on other sites More sharing options...
Sepodati Posted October 17, 2017 Share Posted October 17, 2017 AND there you go. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552788 Share on other sites More sharing options...
benanamen Posted October 17, 2017 Share Posted October 17, 2017 AND there you go. Umm no. How about show me that in Php. Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552789 Share on other sites More sharing options...
Sepodati Posted October 18, 2017 Share Posted October 18, 2017 (edited) 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'; } ?> Edited October 18, 2017 by Sepodati Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552791 Share on other sites More sharing options...
benanamen Posted October 18, 2017 Share Posted October 18, 2017 Ok, @Psycho was ok with the AND suggestion. Too much time in front of the computer. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305366-validation-mixup/#findComment-1552794 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.