2

Upgraded the site from PHP 5.6 to PHP 7.0. After upgrading there was a code error with deprecated eregi(), which I replaced with preg_match() later, but now it outputs error every time I submit a the filled form.

It's probably obvious, but I'm newbie at PHP, just learning.

That's the full code of my contact form:

<?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(); ?>
  • 2
    What is the specific error message? – Jami Gibbs Dec 24 '16 at 21:48
1

Wordpress has a built in function for validating emails and I would advise you use that.

So you can replace your preg_match with the wordpress is_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;
            }

You should also look at the wordpress codex on Data Validation, especially the Input Validation on sanitizing user inputs before using them in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.