Jump to content


Photo

php required forms help


  • Please log in to reply
2 replies to this topic

#1 strackan

strackan
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 March 2006 - 07:56 PM

Hello,

I am relatively new to php/mysql and have been trying to get the following code to work, without success for about a week. Please help a poor newbie.

Here is my initial code:
<?php
// checkBlank.php -- a program to check for blank entries in a form
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Empty Fields</title>
</head>
<body>
<?php
// set up array of label fields
$label_array = array ( "first_name" => "First Name",
                       "middle_name" => "Middle Name",
                       "last_name" => "Last Name",
                       "phone" => "Phone" );
// check each field for blanks
foreach ($_POST as $field => $value)
{
    if ($field != "middle_name")
    {
        if ( !$value )
        {
            $blank_array[$field] = "blank";
        }
    }
}
// if any fields are blank, display error message and form
if (@sizeof($blank_array) > 0) // if blank fields are found
{
    echo "<b>You didn't fill in one or more required fields. You must enter:</b>";
// display list of missing information
    foreach ($blank_array as $field => $value)
    {
        echo "<b><blockquote>$label_array[$field]</blockquote></b>";
    } // end foreach loop
// redisplay form
$first_name=trim(strip_tags($_POST['first_name']));
$middle_name=trim(strip_tags($_POST['middle_name']));
$last_name=trim(strip_tags($_POST['last_name']));
$phone=trim(strip_tags($_POST['phone']));
echo "<p><hr>
    <form action='checkBlank.php' method='POST'>
    <center>
    <table width='95%' border='0' cellspacing='0' cellpadding='2'>
    <tr><td align='right'><b>{$label_array['first_name']}:
    </b></td>
    <td><input type='text' name='first_name' size='65' maxlength='65' value='$first_name' ></td>
    </tr>
    <tr><td align='right'><b>{$label_array['middle_name']}:
    </b></td>
    <td><input type='text' name='middle_name' size='65' maxlength='65' value='$middle_name' ></td>
    </tr>
    <tr><td align='right'><b>{$label_array['last_name']}:
    </b></td>
    <td><input type='text' name='last_name' size='65' maxlength='65' value='$last_name' ></td>
    </tr>
    <tr><td align='right'><b>{$label_array['phone']}:
    </b></td>
    <td><input type='text' name='phone' size='65' maxlength='65' value='$phone' ></td>
    </tr>
    </table>
    <p><input type='submit'
        value='Submit name and phone number' >
        </form>
        </center>";
exit();
}
echo "Welcome to your new account. You may now <a href='login.php'>log in</a>.";
?>
</body>
</html>

When I view this locally, I see:

Welcome to your new account. Please log in.

The form is completely bypassed. So, I tried moving the '}' after the exit(); (line 68) line to the line right before the \\redisplay form comments (line 37). When I do that, I get the form, and it works flawlessly, except when I finally input all the required fields, the form just continues to redisplay. It does not display the welcome message.

In short, I can't get the form to display the welcome message upon successfully inputting all required fields. I'm guessing this is due to a missing ELSE statement or bracket, but I can't for the life of me figure out what I'm missing. Any help would be greatly appreciated.

#2 js_280

js_280
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 27 March 2006 - 07:55 AM

How about using this workaround...

<?php
// checkBlank.php -- a program to check for blank entries in a form
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Empty Fields</title>
</head>
<body>
<?php
// set up array of label fields
$label_array = array ( "first_name" => "First Name",
                       "middle_name" => "Middle Name",
                       "last_name" => "Last Name",
                       "phone" => "Phone" );
// check each field for blanks

@$first_name = trim(strip_tags($_POST['first_name']));
@$middle_name = trim(strip_tags($_POST['middle_name']));
@$last_name = trim(strip_tags($_POST['last_name']));
@$phone = trim(strip_tags($_POST['phone']));

if(strlen($first_name) < 1)
    {
        $blank_array['first_name'] = 'blank';
    }
if(strlen($last_name) < 1)
    {
        $blank_array['last_name'] = 'blank';
    }
if(strlen($phone) < 7)
    {
        $blank_array['phone'] = 'blank';
    }

// if any fields are blank, display error message and form
if (@count($blank_array) > 0) // if blank fields are found
{
    echo "<b>You didn't fill in one or more required fields. You must enter:</b>";
// display list of missing information
    foreach ($blank_array as $field => $value)
    {
        echo "<b><blockquote>$label_array[$field]</blockquote></b>";
    } // end foreach loop
// redisplay form
echo("<p><hr><form action='checkBlank.php' method='POST'><center><table width='95%' border='0' cellspacing='0' cellpadding='2'><tr><td align='right'><b>");

echo($label_array['first_name']);
echo(":</b></td><td><input type='text' name='first_name' size='65' maxlength='65' value='$first_name' ></td></tr><tr><td align='right'><b>{$label_array['middle_name']}:</b></td><td><input type='text' name='middle_name' size='65 'maxlength='65' value='$middle_name' ></td></tr><tr><td align='right'><b>{$label_array['last_name']}:</b></td><td><input type='text' name='last_name' size='65' maxlength='65' value='$last_name' ></td></tr><tr><td align='right'><b>{$label_array['phone']}:</b></td><td><input type='text' name='phone' size='65' maxlength='65' value='$phone' ></td></tr></table><p><input type='submit' value='Submit name and phone number' ></form></center>");
exit;
}
echo "Welcome to your new account. You may now <a href='login.php'>log in</a>.";
?>
</body>
</html>


#3 strackan

strackan
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 March 2006 - 04:44 AM

First off, thanks a lot for your help. I think I'm a lot closer now. I just tried this code, and it seems to work fine, except for one problem:

When the form is first displayed, it gives the blank fields error:

You didn't fill in one or more required fields. You must enter:

First Name

Last Name

Phone


After that it works fine. How can I ensure that the form understands not to display blank field notices when it is first loaded?

Thanks again...






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users