PHP错误处理和将表单数据发送到电子邮件

时间:2015-12-18 14:36:34

标签: php forms validation

最好的PHP验证代码是什么:如果错误数组为空并且提供了电子邮件或电话,那么发送电子邮件并转到确认页面?

以下是代码,但无效。提交表单后,错误array !== 0下面的代码不会执行并移至确认页面。

if(isset($_POST['name_submit']))
       {
        if ($_POST)
        {
        // creating error array

        $errors_goods = array();

            // form validation

                if ($_POST['name_password']!== 6)
                    {
                        $goods_password_error = "*Please enter the correct answer to the security question.";
                        $errors_goods['password_error'] = $goods_password_error;
                    }

                if (empty($_POST['name_name']))
                    {
                        $goods_name_error = "*Please enter the name of the individual or organization looking for goods.";
                        $errors_goods['name_error'] = $goods_name_error;
                    }

                if (empty($_POST['name_goods']))
                    {
                        $goods_goods_error = "*Kind of good(s) e.g. Wheat or Artificial satellite.";
                        $errors_goods['goods_error'] = $goods_goods_error;

                    } 

                if (empty($_POST['name_quantity']))
                    {
                        $goods_quantity_error = "*Enter the quantity required.";
                        $errors_goods['quantity_error'] = $goods_quantity_error;
                    }

                if (empty($_POST['name_email']) && empty($_POST['name_phone']))
                    {
                        $goods_email_error = "*Email / We do not share or pass on your contact detail. OR";
                        $errors_goods['email_error'] = $goods_email_error;
                    }

                if (empty($_POST['name_email']) && empty($_POST['name_phone']))
                    {
                        $goods_phone_error = "*Please enter the phone number if wished to be contacted by phone.";
                        $errors_goods['phone_error'] = $goods_phone_error;
                    }



                    if (count($errors_goods) == 0) 
            {

                $to      = 'gill264@gmail.com';
                $subject = 'goods_goods';
                $message = 'Name: $goods_name \n requires goods_quantity of goods_goods. \n There contact details are:
                e-mail: goods_email \n Phone: goods_phone.';

                mail($to, $subject, $message);
header('Location:Message Confirmation.html');

            }
        }
       }

1 个答案:

答案 0 :(得分:2)

if(empty($errorArray) &&  (!empty($email) || !empty($phone))){
  //send email
}