Jump to content

Need some help with a simple form validation


p3nguattack

Recommended Posts

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!

Link to comment
Share on other sites

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". 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.