升级到PHP 7.0后,我的联系人表单输出错误

时间:2016-12-24 作者:SLembas

将网站从PHP 5.6升级到PHP 7.0。升级后出现代码错误,已弃用eregi(), 我将其替换为preg_match() 后来,但现在每当我提交一份填写好的表单时,它都会输出错误。

这可能很明显,但我是PHP的新手,只是在学习。

这是我联系方式的完整代码:

<?php
    /*
    Template Name: Обратная связь
    */

    if(isset($_POST[\'submitted\'])) {
        if(trim($_POST[\'contactName\']) === \'\') {
            $nameError = \'Please enter your name.\';
            $hasError = true;
        } else {
            $name = trim($_POST[\'contactName\']);
        }
        if(trim($_POST[\'email\']) === \'\')  {
            $emailError = \'Please enter your email address.\';
            $hasError = true;
        } else if (!preg_match("/^[A-Z0-9.%-]+@[A-Z0-9.%-]+.[A-Z]{2,4}$/", trim($_POST[\'email\']))) {
            $emailError = \'You entered an invalid email address.\';
            $hasError = true;
        } else {
            $email = trim($_POST[\'email\']);
        }
        if(trim($_POST[\'comments\']) === \'\') {
            $commentError = \'Please enter a message.\';
            $hasError = true;
        } else {
            if(function_exists(\'stripslashes\')) {
                $comments = stripslashes(trim($_POST[\'comments\']));
            } else {
                $comments = trim($_POST[\'comments\']);
            }
        }
        if(!isset($hasError)) {
            $emailTo = get_option(\'tz_email\');
            if (!isset($emailTo) || ($emailTo == \'\') ){
                $emailTo = get_option(\'admin_email\');
            }
            $subject = "Отзыв от пользователя ".$name;
            $body = "Имя: $name 
    E-mail: $email 

    $comments";
            $headers = \'From: \'.$name.\' <\'.$emailTo.\'>\' . "rn" . \'Reply: \' . $email;

        mail($emailTo, $subject, $body, $headers);
            $emailSent = true;
        }
    } ?>

    <?php get_header(); ?>
    <div class="row">
    <div class="paper">
    <div class="paper-body">
                            <?php if(isset($emailSent) && $emailSent == true) { ?>
       <h2 class="contact-form-thanks">
          Спасибо, ваше сообщение отправлено.
       </h2>
    <?php } else { ?>
    <?php if(isset($hasError) || isset($captchaError)) { ?>
       <p class="error">Извините, произошла ошибка.<p>
    <?php } ?>
    <form action="<?php the_permalink(); ?>" id="contactForm" method="post">

            <input class="contact-form-name" type="text" name="contactName" id="contactName" value="" placeholder="Имя" required>

              <input class="contact-form-email" type="email" name="email" id="email" value="" placeholder="E-mail" required>

           <textarea class="contact-form-text" name="comments" id="commentsText" rows="10" cols="30" value="" placeholder="Сообщение" required></textarea>

           <button type="submit">Отправить</button>
    <button type="reset">Очистить</button>
        <input type="hidden" name="submitted" id="submitted" value="true">
    </form>
    </div>
    </div>
    <?php } ?>
                    </div><!-- .entry-content -->

    <?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Tunji 整理而成

Wordpress有一个用于验证电子邮件的内置功能,我建议您使用它。

所以你可以更换你的preg_match 使用wordpressis_email

else if (!preg_match("/^[A-Z0-9.%-]+@[A-Z0-9.%-]+.[A-Z]{2,4}$/", trim($_POST[\'email\']))) {
            $emailError = \'You entered an invalid email address.\';
            $hasError = true;
        }

REPLACED WITH

else if ( ! is_email( trim( $_POST[\'email\'] ) ) ) {
                $emailError = \'You entered an invalid email address.\';
                $hasError = true;
            }
您还应该查看上的wordpress codexData Validation, 尤其是在代码中使用用户输入之前对其进行清理的输入验证。