PHP表单不确认表单上的错误,也不允许某些字段是可选的

I'm experiencing some issues with a php form i have inherited as part of a project.

The form has to be completed in its entirety to successfully submit, which it shouldn't do as there are optional fields here is no message to advise of the errors.

Any help would be most appreciated. Thanks.

Here's the code snippet.

function validateForm()
        {
            global $error, $data, $errorBox, $successBox, $formShown;
            $formShown = true;
            $error = array();
            $data = array();
            $count = 0;

            $data = $_POST;
            $blocked = array('booooo');
            foreach($_POST as $key=>$value)
            {
                for($i=0;$i<count($blocked);$i++)
                {
                    if(strlen(strstr($value,$blocked[$i]))>0)
                    {
                        $error[$key] = ' class="error"';
                        $count++;
                    }
                }
                if($value == "" || (strlen(trim($value)) == 0))
                {
                    if($key == "submit"){
                        }else{
                    $error[$key] = ' class="error"';
                    $count++;
                        }
                }
                if($key == "phone" && $value<20)
                {
                    $error[$key] = ' class="error"';
                    $count++;
                }
            } 
            // function to confirm form submission
            if($count>0)
            {
                $echo "Please enter all the areas highlighted in red"
            }
            else
            {
                $this->sendForm();
                $successBox = '<div class="successBox"><h3>Your e-mail has been sent.</h3></div>';
            }

        //Get the uploaded file information
                $name_of_uploaded_file =
                basename($_FILES['logo']['name']);

        //get the file extension of the file
                $type_of_uploaded_file =
                substr($name_of_uploaded_file,
                strrpos($name_of_uploaded_file, '.') + 1);

                $size_of_uploaded_file =
                $_FILES["logo"]["size"]/1024;//size in KBs

        //attachment settings
                $max_allowed_file_size = 1024; // size in KB
                $allowed_extensions = array("jpg", "jpeg", "gif", "bmp");

        //Validations
                if($size_of_uploaded_file > $max_allowed_file_size )
                {
                  $errors .= "
 Size of file should be less than $max_allowed_file_size";
                }

        //------ Validate the file extension -----
                $allowed_ext = false;
                for($i=0; $i<sizeof($allowed_extensions); $i++)
                {
                      if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
                  {
                    $allowed_ext = true;
                  }
                }

                if(!$allowed_ext)
                {
                  $errors .= "
 The uploaded file is not supported file type. ".
                  " Only the following file types are supported:  ".implode(',',$allowed_extensions);
                }
        //copy the temporary uploaded file to uploads folder
                $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; //NEED TO CREATE A FOLDER WITH 777 PERMISSIONS TO DROP THE FILE IN
                $tmp_path = $_FILES["uploaded_file"]["tmp_name"];

                if(is_uploaded_file($tmp_path))
                {
                  if(!copy($tmp_path,$path_of_uploaded_file))
                  {
                    $errors .= '
 error while copying the uploaded file';
                  }
                }

        }

        function sendForm()
        {
            global $data, $formShown;
            require_once('email.class.php');

            date_default_timezone_set('Europe/London');

            $data['timeSent'] = date('l, jS F, Y \a\t H:i',time());

// Send email with enquiry details

            $contactemail = new Email();
            $contactemail->setTemplate("quickcontact");
            $contactemail->setTo('jonathan@email.com');  // or mike@email.com?
            $contactemail->setSubject("My enquiry");
            //$contactemail->setFrom($data['name'],$data['email']);
            $contactemail->setFrom("My Enquiry", "jonathan@email.com"); // double check
            $contactemail->sendEmail($data);
            //attach the file to the email
            //$contactemail->addAttachment($path_of_uploaded_file);

            // Send email to the visitor to confirm the contact form they sent
            $contacteeemail = new Email();
            $contacteeemail->setTemplate("quickcontactee");
            $contacteeemail->setTo($data['email']);
            $contacteeemail->setSubject("My");
            $contacteeemail->setFrom("My", "jonathan@email.com"); // double check
            $contacteeemail->sendEmail($data);

            $this->unsetData();
            $formShown = false;
        }

Here's the HTML containing the optional fields.

<div class="row orange-underline">
            <div class="addressArea hidden">
            <div class="duplicateFree">
                <label>Free Standing Midwifery Unit:</label>
                    <input <?=$error["freeStandingUnit"]; ?> type="text" name="freeStandingUnit" id="freeStandingUnit" value="<?=$_POST['freeStandingUnit'] ?>"/>
                    <label>Free Standing Midwifery Address:</label>
                        <textarea <?=$error["freeStandingAddresses"]; ?> name="freeStandingAddresses" id="freeStandingAddresses" rows="0" cols="0" ><?=$_POST['freeStandingAddresses'] ?></textarea>
                    <label>Free Standing Midwifery Postcode:</label>
                    <input <?=$error["freeStandingPost"]; ?> type="text" name="freeStandingPost" id="freeStandingPost" value="<?=$_POST['freeStandingPost'] ?>"/>
                    <label>Distance To Main Site:</label>
                        <input <?=$error["freeMainSiteDistance"]; ?> type="text" name="freeMainSiteDistance" id="freeMainSiteDistance" value="<?=$_POST['freeMainSiteDistance'] ?>"/>
                    <label>Non-urgent Transfer Time:</label>
                        <input <?=$error["freeNonUrgentTransfer"]; ?> type="text" name="freeNonUrgentTransfer" id="freeNonUrgentTransfer" value="<?=$_POST['freeNonUrgentTransfer'] ?>"/>
                    <label>Blue Light Transfer Time:</label>
                        <input <?=$error["freeBlueLightTransfer"]; ?> type="text" name="freeBlueLightTransfer" id="freeBlueLightTransfer" value="<?=$_POST['freeBlueLightTransfer'] ?>"/>
            </div>
            </div>
            </div>
            <div class="row">
                <span class="leftSpan"> <span><label>Any Alongside Midwifery Units:</label>
                                        <p>
                                            <select class="region_units">
                                                 <option value="select">Select</option>
                                    <?php for($i = 1; $i <= 10; $i++) { ?>
                                        <option value="<?php echo $i; ?>"><?php echo $i . ($i === 1 ? ' unit' : ' units'); ?></option>
                                    <?php } ?>
                                            </select>
                                       </p>
                                </span> </span>
                                </div>
                            <div class="row orange-underline">
            <div class="addressArea2 hidden">
            <div class="duplicate">
                                <label>Alongside Midwifery Unit:</label>
                                <input <?=$error["alongsideUnit"]; ?> type="text" name="alongsideUnit" id="alongsideUnit" value="<?=$_POST['alongsideUnit'] ?>"/>
                <label>Alongside Midwifery Address:</label>
                                        <textarea <?=$error["alongsideAddresses"]; ?> name="alongsideAddresses" id="alongsideAddresses" rows="0" cols="0" ><?=$_POST['alongsideAddresses'] ?></textarea>
                                    <label>Alongside Midwifery Postcode:</label>
                    <input <?=$error["alongsideUnitPost"]; ?> type="text" name="alongsideUnitPost" id="alongsideUnitPost" value="<?=$_POST['alongsideUnitPost'] ?>"/>
                <label>Distance To Main Site:</label>
                    <input <?=$error["alongsideMainSiteDistance"]; ?> type="text" name="alongsideMainSiteDistance" id="alongsideMainSiteDistance" value="<?=$_POST['alongsideMainSiteDistance'] ?>"/>
                <label>Non-urgent Transfer Time:</label>
                    <input <?=$error["alongsideNonUrgentTransfer"]; ?> type="text" name="alongsideNonUrgentTransfer" id="alongsideNonUrgentTransfer" value="<?=$_POST['alongsideNonUrgentTransfer'] ?>"/>
                <label>Blue Light Transfer Time:</label>
                    <input <?=$error["alongsideBlueLightTransfer"]; ?> type="text" name="alongsideBlueLightTransfer" id="alongsideBlueLightTransfer" value="<?=$_POST['alongsideBlueLightTransfer'] ?>"/>
                            </div>
            </div>
            </div>
                            <div class="row">
                                <span class="leftSpan"> <span><label>Any Extra Addresses:</label>
                <p>
                                        <select class="extra_region_units">
                                          <option value="select">Select</option>
                                                <?php for($i = 1; $i <= 10; $i++) { ?>
                                                        <option value="<?php echo $i; ?>"><?php echo $i . ($i === 1 ? ' unit' : ' units'); ?></option>
                                                <?php } ?>
                                        </select>
                                    </p>
                                </span> </span>
            </div>
            <div class="row orange-underline">
            <div class="addressArea3 hidden">
            <div class="duplicateExtra">
                                <label>Extra Unit:</label>
                <input <?=$error["extraUnit"]; ?> type="text" name="extraUnit" id="extraUnit" value="<?=$_POST['extraUnit'] ?>"/>
                                <label>Extra Unit Address:</label>
                <textarea <?=$error["extraAddresses"]; ?> name="extraAddresses" id="extraAddresses" rows="0" cols="0" ><?=$_POST['extraAddresses'] ?></textarea>
                                <label>Extra Unit Postcode:</label>
                                    <input <?=$error["extraUnitPost"]; ?> type="text" name="extraUnitPost" id="extraUnitPost" value="<?=$_POST['extraUnitPost'] ?>"/>
                                <label>Distance To Main Site:</label>
                <input <?=$error["extraMainSiteDistance"]; ?> type="text" name="extraMainSiteDistance" id="extraMainSiteDistance" value="<?=$_POST['extraMainSiteDistance'] ?>"/>
                                <label>Non-urgent Transfer Time:</label>
                <input <?=$error["extraNonUrgentTransfer"]; ?> type="text" name="extraNonUrgentTransfer" id="extraNonUrgentTransfer" value="<?=$_POST['extraNonUrgentTransfer'] ?>"/>
                                <label>Blue Light Transfer Time:</label>
                <input <?=$error["extraBlueLightTransfer"]; ?> type="text" name="extraBlueLightTransfer" id="extraBlueLightTransfer" value="<?=$_POST['extraBlueLightTransfer'] ?>"/>

            </div>
            </div>
            </div>

You are missing a ' which could cause an issue in your PHP code.

Replace this:

$successBox = <div class="successBox"><h3>E-Mail Sent.</h3></div>';

With this:

$successBox = '<div class="successBox"><h3>E-Mail Sent.</h3></div>';

Error in your code

$this->sendForm();
$successBox = <div class="successBox"><h3>E-Mail Sent.</h3></div>';

Single quote is missing it should be like this

$this->sendForm();
$successBox = '<div class="successBox"><h3>E-Mail Sent.</h3></div>';