邮件表单发送成功消息并检查输入是否为空

时间:2017-10-10 12:04:12

标签: php forms email

所以我正在制作联系表单,当我们点击提交时,它应该向管理员发送包含输入的电子邮件。电子邮件功能已经工作,我缺少的是验证输入字段是否为空。我尝试在输入标记中添加必需的属性。它适用于localhost,但由于某种原因不在服务器上。此外,它需要弹出一条消息,确认您已提交表单。我在这里使用了 onclick 功能。但是我无法澄清它是否有效,因为它只需要在提交成功且没有空字段时弹出消息。

这是我使用的代码。联系已经建立。

<!---Mail Starts-->
<?php
if (isset($_POST['submit'])) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Client<' . $_POST['mail'] . '>' . "\r\n";
$headers .= 'Reply-To: ' . $_POST['mail'] . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";

$to = 'email@gmail.com';
$subject = "Send Us A Smile - ".$_POST['name'];
$message = "
<html>
<body>
<div style='font-family:arial;width:100%;padding:5px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;'>
    <div style='margin:0 auto;max-width:500px;padding:5px;text-align:center;background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#741C57), to(#360d29));background: -moz-linear-gradient(90deg, #360d29, #741C57);color:#fff;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;border-radius:15px;'>
    </div>
    <div style='text-align:center;padding:5px;color:#666;background-color:rgba(255,255,240,1);max-width:460px;margin:0 auto; border:2px solid #000;border-radius:0 0 5px 5px;'>
        <h2 style='font-weight:200;line-height:.3em;'>
            Send Us A Smile <br>
        </h2>

        <p style='text-align:left;'>
            Name : " . $_POST['name'] . "<br>
            Email : " . $_POST['mail']  . "<br>
            Subject : " . $_POST['subject'] . "<br>
            Message : " . $_POST['message'] . "
        </p>
    </div>
</div>
</body>
</html>
";

mail($to,$subject,$message,$headers);

}
?>
<!---Mail Ends-->
<div class="full" id="contactForm">
            <div>
            <form name ="contact" action="" method="POST">
                        <input type="hidden" name="recipient" value="info@email.com"> 
                        <input type="hidden" name="subject" value="Webmail">
                        <div class="eight-eight">
                            <label class="two-eight" for="name">Name</label>
                            <input class="six-eight" type="text" name="name" placeholder="Your name" required oninvalid="this.setCustomValidity('Please enter your Name here')" oninput="setCustomValidity('')"/>
                        </div>
                        <div class="eight-eight">
                            <label class="two-eight" for="name">Subject</label>
                                <select id="subject" name="subject">
                                    <option value="">Choose  a service..</option>
<option value="">Option 1..</option>
<option value="">Option 2..</option>
                                    <option value="others">Others</option>
                                </select>
                        </div>

                        <div class="eight-eight">
                            <label class="two-eight" for="name">Message</label>
                            <textarea class="six-eight" placeholder="Your message" name="message" required oninvalid="this.setCustomValidity('May we know what your message is?')" oninput="setCustomValidity('')"/></textarea>
                        </div>

                        <div class="eight-eight">
                            <label class="two-eight" for="name">Email</label>
                            <input class="six-eight" type="mail" name="mail" placeholder="Your email address" required oninvalid="this.setCustomValidity('Please enter your Email Address here')" oninput="setCustomValidity('')"/>
                        </div>

                        <input type="submit" name = "submit" value="Send" onclick="hgsubmit()">
                        <input type="hidden" name="redirect" value="http://www.email.com"> 
            </form>
            </div>
        </div>

有不同的方法吗?

1 个答案:

答案 0 :(得分:-1)

这是我用作内部开发工具一部分的验证器。它是服务器端,是更大框架的一部分,但它对你来说还可以。

    <?php

/*
 *
 * @Class Name:InputValidation
 * @Framework:Mlisoft MLM 4.0
 * @Date Created: 18, August, 2017
 * @Version: 1.0
 * @Contributor: Adeniyi Anthony A <a.anthony@mlisoftinc.com>
 * @mlisoftinc@gmail.com
 *
 */

class InputValidation
{
    /*
     * ----------------------------------------------------------------
     * Validate form input
     * ----------------------------------------------------------------
     */

    /*
     * Validate form input
     * @param   array    $data
     * @param   array    $exceptionList
     * @param   array    $alias
     * @param   array    $filters
     * @return  array
    */
    public function validateInputs(array $data, array $exceptionList = null, array $alias = null, array $filters = null)
    {
        if ($data == null || $data == "" || !$data || !is_array($data)) {
            return array(
                'status' => false,
                'msg' => 'Invalid data provided',
            );
        }
        $errorList = null;
        foreach ($data as $curData => $value) {
            $exceptCurrent = false;

            if ($value == "") {
                /*
                 * check if this is part of the exception
                 */
                if ($exceptionList !== null) {
                    foreach ($exceptionList as $curExcept) {
                        /*
                         * case insensitive search
                         */
                        if ($curData == $curExcept) {
                            $exceptCurrent = true;
                            break;
                        }
                    }
                }

                if ($exceptCurrent === false) {
                    if ($alias != null && isset($alias[$curData])) {
                        $name = $alias[$curData];
                    } else {
                        $name = $curData;
                    }
                    $errorList[] = array(
                        'name' => $name,
                        'value' => $value,
                    );
                }
            }

            /*
             * finally, lets validate filters if they are passed
             */
            if ($filters != null && isset($filters[$curData])) {
                /*
                 * check the filter validations
                 */
                $curFilter = $filters[$curData];
                /*
                 * go over the filter, and validate each filter
                 * parameter provided
                 */
                foreach ($curFilter as $key => $paramValue) {
                    if (!isset($name)) {
                        $name = $curData;
                    }

                    if ($key == "must") {
                        /*
                         * validate must contain the exact value provided
                         * can use regular expression later
                         */
                        if (!preg_match("/" . $paramValue . "/", $value)) {
                            $msg = "value must contain $paramValue";
                            $errorList[] = array(
                                'name' => $name,
                                'value' => $value,
                                'msg' => $msg,
                            );
                        }
                    }
                }
            }
        }

        /*
         * attempt tp build a long error message
         */
        $msg = null;
        if (isset($errorList) && $errorList !== null) {
            $msg = "Invalid input. Ensure all required form field are entered<br>";
            foreach ($errorList as $error) {
                if (isset($error['msg'])) {
                    $msg = $msg . $error['name'] . " " . $error['msg'];
                } else {
                    $msg = $msg . $error['name'] . " cannot be empty<br>";
                }
            }
        }
        /*
         * return complete validation
         */
        if ($errorList === null) {
            return array(
                'status' => true,
                'msg' => 'Success',
                'error' => false,
                'error_list' => $errorList,
                'error_msg' => null,
            );
        } elseif ($errorList !== null) {
            return array(
                'status' => false,
                'msg' => 'Failed',
                'error' => true,
                'error_list' => $errorList,
                'error_msg' => $msg,
            );
        }
    }

}

这样的呼叫验证:

$validator = new InputValidation();
        $val = $validator->validateInputs($data);

        if (isset($val['error']) && $val['error'] == true) {
            // do whatever
            );
        }

验证器还接受其他可选参数,如

$ exceptionList ['formField','anotherFormFiled']    用于免除某些字段的验证

$ alias传递为数组('formField'=&gt;'Form Filed Caption')    用于显示自定义字段标题

$ filters as ['email'=&gt;数组('must'=&gt;“@”)]   用于验证输入。例如电子邮件