p3nguattack Posted August 3, 2012 Share Posted August 3, 2012 Hello, I'm new to this forum, and PHP, so don't go too rough on me lol. I'm learning from tutorials, and everywhere I look the code for "Not equal" is != correct? Well in my script, I'm trying to make required fields with 0 being an exception (for radio buttons) and for some reason when I put in != my form lets me leave the field blank and I get the "form successfully updated" message I'm supposed to get when the form is updated. Here's the code $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $fieldname; } } However if I switch them around to =! I can no longer leave the field blank, and get the error message as I'm supposed to. I'm not sure if this is a PHP update since the tutorial was made or what, but I'd like to have it cleared up because, !empty and !isset work as they're supposed to, and me being new to this, I'm not sure if coding it as =! will act differently on the client end than I had planned. If that is in fact how not equal is supposed to be coded though, I'd like to know to clear up this confusion lol. Thanks in advance for any replies. I appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/ Share on other sites More sharing options...
ManiacDan Posted August 3, 2012 Share Posted August 3, 2012 if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { Break this code down: if ( !isset($_POST[$fieldname]) || ( empty($_POST[$fieldname]) && $_POST[$fieldname] != 0 ) ) { So we have: If it's not set OR (it's empty and it's not equal to zero). Looks like you want the field to be required, and not zero, right? Required and not zero: if ( !isset($_POST[$fieldname]) || empty($_POST[$fieldname]) || $_POST[$fieldname] == 0 ) If zero is an allowable value, just remove the last bit. Also, =! is not an operator, so I don't know why you would "switch that up". Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366572 Share on other sites More sharing options...
Psycho Posted August 3, 2012 Share Posted August 3, 2012 Here's my take. PHP is a loosely types language. This means that different values can resolve to the same thing. You have this in your comparison $_POST[$fieldname] != 0 That is checking if the value IS NOT equal to the INTEGER of 0. In PHP a 0 is also considered false. false is also equivalent to an unset value when using empty()! So, two things: 1 - you should have used $_POST[$fieldname] !== '0' To check that that value was not EXACTLY a string with the value of '0'. 2 - Even that would have flaws and I would suggest a different approach. For example, if the user only entered spaces the validation would pass because it would not be an empty value. I have to assume you don't want someone to enter just spaces. So, I would suggest trimming the value first (if set), then only checking that the value is not an empty STRING [i.e. don't use empty()]. foreach($required_fields as $fieldname) { $fieldValue = isset($_POST[$fieldname]) ? trim($_POST[$fieldname]) : ''; if ($fieldValue=='') { $errors[] = $fieldname; } } Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366586 Share on other sites More sharing options...
p3nguattack Posted August 3, 2012 Author Share Posted August 3, 2012 I think you both misunderstand the reason I have behind this. I want 0 (false) to be an allowable answer. I know the form is flawed as it's used to validate both a string field, and radio buttons for whether or not the page is visible. So on is true, and off is false. before I added this piece to my code. If I were to select the visibility to be off, it wouldn't query mysql because php considered it an empty value therefore not sending it along with the rest of the query. It used to look like. $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]))) { $errors[] = $fieldname; } } When I added && $_POST[$fieldname] != 0) it would allow me to send the false back to the database without any issues, but it also would let me send a blank field. Maybe this clears up my question a bit. Also, could you explain the difference between != and !==. I don't think !== is what I'm looking for because the way you said it, it should be referring to a string, but that's not why this code is in place. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366684 Share on other sites More sharing options...
jazzman1 Posted August 3, 2012 Share Posted August 3, 2012 Psycho told you why ? Just wrap 0 up with single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366687 Share on other sites More sharing options...
Pikachu2000 Posted August 3, 2012 Share Posted August 3, 2012 Trim the $_POST array values, check to see that each required field is set, and has a positive length. $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { trim($_POST[$fieldname]); if ( !isset($_POST[$fieldname]) && strlen($_POST[$fieldname]) < 1 ) { $errors[] = $fieldname; } } Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366690 Share on other sites More sharing options...
p3nguattack Posted August 4, 2012 Author Share Posted August 4, 2012 My fault, I misunderstood, not the other way around . I thought putting the 0 in single quotes would make mysql consider it a string instead of an integer. As for Pikachu's(and I notice also Psycho) suggestion, I have the specific variable (menu_name(the only one that isn't an integer on the form)) trimmed on a separate part of the code, but thanks for the suggestion. I also have a separate function for max length. I am curious though, that <1 would allow the integer 0 and forbid the field from being blank as well correct? I never thought to do it that way and the trim on it already should stop the user from submitting a blank field with a couple spaces as psycho suggested. These are all great ideas, and I appreciate all the support. I got it working with the single quotes, so thank you Psycho for that, and thanks everyone else for your alternate suggestions. I'm slowly learning there is more than one way to do almost everything with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366729 Share on other sites More sharing options...
ManiacDan Posted August 5, 2012 Share Posted August 5, 2012 I thought putting the 0 in single quotes would make mysql consider it a string instead of an integer. MySQL is not at all involved in this. Also, like I said, simply removing the last condition from my code would have allowed any input as long as an input was provided. I am curious though, that <1 would allow the integer 0 and forbid the field from being blank as well correct?No. The LENGTH is being compared to 1, not the value. Quote Link to comment https://forums.phpfreaks.com/topic/266628-need-some-help-with-a-simple-form-validation/#findComment-1366890 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.