Jump to content

Recommended Posts

I have an order form script, and when a user does not fill out a field, it shows an error message on a new page... one at a time.

 

For example, if you forgot to fill in your email, and your name, it would show "please fill in your name  Go Back".  Then you fill in your name, and resubmit, it will say "please fill in your email  Go Back", and so on.

 

I would like to be able to list all the required fields at once when a user forgets to fill them in. Here is my current code:

<?php
/* Set e-mail recipient */
$myemail              = "support@zip-cover.com";

/* Check all form inputs using check_input function */
$subject              = check_input($_POST['subject'], "Please enter your name");
$email                = check_input($_POST['email'], "Please enter your email");
$phone                = check_input($_POST['phone']);
$street               = check_input($_POST['street'], "Please enter your street address");
$city                 = check_input($_POST['city'], "Please enter your city");
$state                = check_input($_POST['state'], "Please enter your state/province");
$country              = check_input($_POST['country'], "Please enter your country");
$postal               = check_input($_POST['postal'], "Please enter your zip code");
$Manufacturer         = check_input($_POST['Manufacturer'], "Please enter your boat manufacturer");
$Year                 = check_input($_POST['Year']);
$BoatClass            = check_input($_POST['BoatClass'], "Please choose your boat class");
$BoatModel            = check_input($_POST['BoatModel'], "Please choose your boat model");
$CoverModel           = check_input($_POST['CoverModel']);
$CoverQuantity        = check_input($_POST['CoverQuantity']);
$CoverBoatModel       = check_input($_POST['CoverBoatModel']);
$RigQuantity          = check_input($_POST['RigQuantity']);
$Fabric               = check_input($_POST['Fabric']);
$OarBag               = check_input($_POST['OarBag']);
$MainColor            = check_input($_POST['MainColorN']);
$TipColor             = check_input($_POST['TipColorN']);
$MainColor            = check_input($_POST['MainColorP']);
$TipColor             = check_input($_POST['TipColorP']);
$MainColor            = check_input($_POST['MainColorS']);
$TipColor             = check_input($_POST['TipColorS']);
$requests             = check_input($_POST['requests']);
$how                  = check_input($_POST['how']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message = "Zip-Cover.com order form

From:
$subject
$email
$phone

Shipping Address:
$street
$city, $state
$country $postal

Boat Type
Manufacturer: $Manufacturer
Year Built: $Year
Boat Class: $BoatClass
Boat Model: $BoatModel

Quantity       Product

$CoverQuantity   of   $CoverModel

$RigQuantity   of   Rigger Bag

$OarQuantity   of   Oar Bag

Fabric
$Fabric

Colors- Make sure colors corrospond to fabric choice. Ignore any other colors.
Nylon Main Color: $MainColorN
Nylon Tip Color: $TipColorN

Polyester Main Color: $MainColorP
Polyester Tip Color: $TipColorP

Sunbrella Main Color: $MainColorS
Sunbrella Tip Color: $TipColorS

Special Requests
$requests

How They Found Us:
$how

zip-cover.com
";

/* Send the message using mail() function */
mail($myemail, $subject, $message, "From: $email");

/* Redirect visitor to the thank you page */
header('Location: recieves.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Fields with red astericks are required. <br />Please correct the following error:</b><br />
    <?php echo $myError; ?><br />
    <a href="javascript:history.go(-1)">Go back</a>

    </body>
    </html>
<?php
exit();
}
?>

 

Any help would be appreciated greatly.

Thanks in advance,

mermyphp

Link to comment
https://forums.phpfreaks.com/topic/184900-php-form-error-list/
Share on other sites

well first off all, your show_error() function

    <html>
    <body>

    <b>Fields with red astericks are required. <br />Please correct the following error:</b><br />
    <?php echo $myError; ?><br />
    <a href="javascript:history.go(-1)">Go back</a>

    </body>
    </html>

 

You should leave out the html/body tags. First off, if you have multiple errors (which you might) you will have multiple html/body tags, which not only is invalid html, but will prevent you from seeing other stuff (because the main body tags, which are the first that appear, have been closed).

Link to comment
https://forums.phpfreaks.com/topic/184900-php-form-error-list/#findComment-976080
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.