Page 1 of 1

Quick Question

Posted: Sun Aug 17, 2008 8:18 pm
by Skullgrabber
Hey everyone,
I'm very new to PHP, but I've been exploring with it and I've found it to be very interesting so far, to say the least. My latest 'project' has been trying to make a forum in which you fill out text boxes. But my problem is with telling which boxes have been filled in or not. Here's my code: (not posting the body)

Code: Select all

 
<?
if($_POST['submit']) 
{
    $stuff = array("username","password","firstname","lastname","country","state","town","zip","email");
    $i = 0;
    $notfilled = 0;
    while ($i < <!-- s8) --><img src=\"{SMILIES_PATH}/icon_cool.gif\" alt=\"8)\" title=\"Cool\" /><!-- s8) -->
    {
        if($_POST[$stuff[i]] != NULL)
        {
            //it's all good....
        }
        else if($_POST[$stuff[i]] == NULL)
        {
            $notfilled++;
        }
        $i++;
    }
    if($notfilled > 0)
    {
        //echo "One or more fields were not filled out. Please fill them in to continue.";
    }
    echo $notfilled;
}
?>
 
 
For some reason the variable '$notfilled' always returns eight, but I want it to return the number of fields left blank. If anyone can help me, that'd be great. Thanks.

Re: Quick Question

Posted: Sun Aug 17, 2008 10:46 pm
by lightnb
It looks like something strange got into your while loop, so it's hard to tell why your code isn't working.

How about:

Code: Select all

 
if(isset($_POST['submit']))
{
    $FieldsArray = array('username', 'password', 'firstname', 'lastname', 'country', 'state', 'town', 'zip', 'email');
 
    $EmptyFields = 0;
    foreach($FieldsArray AS $FieldName)
    {
        if(!isset($_POST[$FieldName]))
        { // Field is not filled in, increment counter
            $EmptyFields++;
        }
    }
    if($EmptyFields != 0)
    {
        echo $EmptyFields . ' fields were not filled out. Please fill them in to continue.';
    }
}
?>
 
Changes:

Use foreach() to loop through arrays
You can use !isset() or empty() to check if a variable exists and has a non-zero value. I don't think "!= null" does what you want here. Check the PHP manual to determine which is appropriate for your application.

You should also use single quotes for strings, since they parse faster. If you use double quotes, PHP will search the string for variables to parse, even if there aren't any!

I also changed " > 0" to " != 0". Because that's what you really want to know ;)

I would also recommend validating each field separately. I know it's a pain, but it makes it easier for your users, and also makes sure that the data entered is valid. Regular expressions are your friend here.

Also, If user names are unique in your database, and you're inserting the data from this form, you'll need to query to see if the user name is already taken, before attempting the insert. (or you'll get a big ugly 'duplicate entry exists....' MySQL error)

Re: Quick Question

Posted: Mon Aug 18, 2008 12:08 am
by dancing dragon
Is it because you typed $stuff instead of $stuff[$i]? (In two places.)

Re: Quick Question

Posted: Mon Aug 18, 2008 8:20 am
by Skullgrabber
dancing dragon wrote:Is it because you typed $stuff instead of $stuff[$i]? (In two places.)


Thanks dancing dragon, it worked. I'm just used to a different language :oops: