Jump to content

php required forms help


strackan

Recommended Posts

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:
[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>
[/code]

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.
Link to comment
Share on other sites

How about using this workaround...

[code]
<?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>
[/code]
Link to comment
Share on other sites

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:

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

First Name

Last Name

Phone[/i]

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

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.