Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 邮件表单发送成功消息并检查是否输入!空的_Php_Forms_Email - Fatal编程技术网

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

Php 邮件表单发送成功消息并检查是否输入!空的,php,forms,email,Php,Forms,Email,所以我正在制作一个联系表单,当我们点击提交时,它会向管理员发送一封电子邮件,包括输入内容。电子邮件功能已经在运行,我缺少的是检查输入字段是否为空的验证。我尝试在输入标记中添加必需的属性。它可以在本地主机上工作,但由于某些原因不能在服务器上工作。此外,它还需要弹出一条消息,确认您已提交表单。我在这里使用了onclick函数。但我无法澄清它是否有效,因为它只需要在提交成功且没有空字段时弹出消息 这是我使用的代码。已建立连接 <!---Mail Starts--> <?php if

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

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

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

$alias pass作为数组'formField'=>'Form field Caption' 用于显示自定义字段标题

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

当然有很多方法可以做到这一点,但这需要你自己编写一些代码。对于验证,您不能隐式信任客户端验证。您可能希望包含服务器端代码以检查是否提交了值。例如,您当前检查isset$\u POST['submit'],查看表单是否已发布。您可以执行类似的检查,以查看表单中是否发布了特定数据元素,您可以检查发布字符串的长度等。构建逻辑以执行检查并相应地发送或不发送消息。
$validator = new InputValidation();
        $val = $validator->validateInputs($data);

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