繁体   English   中英

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

[英]Mail Form Sends Success Message and checks if input !empty

所以我正在制作一个联系表格,当我们点击提交时,它应该向管理员发送一封电子邮件,包括输入。 电子邮件功能已经在工作,我缺少的是检查输入字段是否为空的验证。 我尝试在输入标签中添加所需的属性。 它适用于本地主机,但由于某种原因不适用于服务器。 此外,它需要弹出一条消息,确认您已提交表单。 我在这里使用了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>

有没有不同的方法来做到这一点?

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

    <?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 pass as array('formField' => 'Form Filed Caption') 用于显示自定义的某些字段标题

$filters as ['email' => array('must' => "@")] 用于验证输入。 例如电子邮件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM