0

we are receiving this error:

Warning: preg_match(): No ending delimiter '^' found in /home/goose/public_html/wp-content/themes/goose/partials/contact.php on line 32 Please enter a valid email address

http://thegoosedarien.com - using the contact form, we receive this error message when submitting.

No idea how to resolve this. What is missing from the code?

<?php 

$asset_path = get_bloginfo('template_url');

?>

<div class="left">

<span class="main-title">
    <?php _e( 'Contact Us', 'goose' ); ?>
</span>

<?php 

    if($_SERVER['REQUEST_METHOD'] === 'POST') { 

        $flag = 1; 

        echo '<div class="notice">';

        if($_POST['submission_name'] == '') { 
            $flag = 0; 
            echo "Please enter your name<br />";
        } else if(!preg_match('/[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*/', 
$_POST['submission_name'])) { 
            $flag=0; 
            echo "Please enter a valid name<br />"; 
        } 

        if($_POST['submission_email'] == '') { 
            $flag = 0; 
            echo "Please enter your email address<br />";
        } else if(!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a- 
   z0-9-]+)*(.[a-z]{2,3})$", $_POST['submission_email'])) { 
            $flag=0; 
            echo "Please enter a valid email address<br />";
        } 

        if($_POST['submission_message'] == '') { 
            $flag = 0; 
            echo "Please enter a message<br />";
        }

        if (empty($_POST)) { 

            echo "Sorry, your submission is invalid - please try again!"; 
            exit; 

        } else { 

            if($flag == 1) { 
                wp_mail(get_option("admin_email"), 
trim($_POST[submission_name])." sent you a message from " . 
get_option("blogname"), stripslashes(trim($_POST[submission_message])),"From: ".trim($_POST[submission_name])." <".trim($_POST[submission_email]).">\r\nReply-To:".trim($_POST[submission_email]));
                echo "Submission successfully sent!<br />";
            }

        }

        echo '</div>';

    }

?>

<form method="post" parsley-validate parsley-show-errors="false" 
id="contact-form">
    <div class="line">
        <label for="name"><?php _e( 'Your Name', 'goose' ); ?></label>
        <input type="text" id="name" name="submission_name" required="required" />
    </div>
    <div class="line">
        <label for="email"><?php _e( 'Your Email', 'goose' ); ?></label>
        <input type="text" id="email" name="submission_email" required="required" parsley-type="email" />
    </div>
    <div class="line">
        <label for="msg"><?php _e( 'Message', 'goose' ); ?></label>
        <textarea id="msg" cols="10" rows="10" name="submission_message" required="required"></textarea>
    </div>  
    <button>
        <span><?php _e( 'Submit', 'goose' ); ?></span>
    </button>
</form>

0

I usually try to avoid using preg_match(), because I get the pattern right very rarely. Perhaps you could try using some other functions to do the validating. For example PHP's ctype_alnum() for the name field and WP's is_email() for the email field. Like so,

if ( $_POST['submission_name'] == '' ) { 
  $flag = 0; 
  echo "Please enter your name<br />";
} else if ( ! ctype_alnum( $_POST['submission_name'] ) ) { // ctype_alnum returns TRUE if every character in text is either a letter or a digit, FALSE otherwise. 
  $flag = 0; 
  echo "Please enter a valid name<br />"; 
} 

if ( $_POST['submission_email'] == '' ) {
  $flag = 0; 
  echo "Please enter your email address<br />";
} else if ( ! is_email( $_POST['submission_email'] ) ) {
  $flag = 0; 
  echo "Please enter a valid email address<br />";
}

Also, if you want to make the wp_mail() function a little bit more readable, you could do something like this,

$name = sanitize_text_field( $_POST['submission_name'] );
$email = sanitize_email( $_POST['submission_email'] );
$subject = sprintf( 
  '%s sent you a message from %s',
  $name,
  get_option("blogname")
);
$message = sanitize_textarea_field( $_POST['submission_message'] );
$from = sprintf(
  "From: %s <%s>\r\n",
  $name,
  $email,
);
$reply_to .= sprintf(
  "Reply-To: %s",
  $email,
);
$headers = $from . $reply_to;
wp_mail(
  get_option("admin_email"),
  $subject,
  $message,
  $headers,
);
  • prntscr.com/nt9n6r After implimenting the changes for the top recommendation, these errors occurred. What would be replaced with the wp_mail suggestion, beginning where? – Louis Procaccini May 25 at 18:04
  • About the errors, you're just missing some backtics on the code you posted. E.g this $_POST[submission_name] should be $_POST['submission_name']. – Antti Koskinen May 25 at 18:15
  • @LouisProcaccini Replace the the wp_mail function inside if($flag == 1) statement with the suggestion I posted. So the first three lines inside the if, but leave the success message line starting with echo. – Antti Koskinen May 25 at 18:20
  • Perfect. No errors. Let me try to replace the wp_mail function now. – Louis Procaccini May 25 at 18:20
  • It's just a matter of taste and its not strictly necessary. – Antti Koskinen May 25 at 18:22

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.