I’m probably missing something really simple here or have a little typo in my code but the PHP form I’ve created for a WordPress site I’m building won’t send. If there are errors in the form, the validation works (except for if you just enter a name) but when you fill in the form correctly and click submit, it simply goes to the page it’s meant to for processing and says ‘Sorry, no posts matched your criteria.’. Plus the mail doesn’t get sent.
EDIT
The link to the dev site is http://dev.garethdaine.com/inkframe/.
The page for processing does exist. Here’s my code:
Form Code
<form name="quick-contact" class="quick-contact" method="post" action="<?php bloginfo('url'); ?>/get-in-touch/">
<h3>Request a Call Back</h3>
<h4><label for="name">Name</label></h4>
<input id="name" name="name" type="text" />
<h4><label for="email">Email</label></h4>
<input id="email" name="email" type="text" />
<h4><label for="phone">Phone</label></h4>
<input id="phone" name="phone" type="text" />
<input id="submit" type="submit" value="Submit" />
<input type="hidden" name="submitted" id="submitted" value="true" />
<input type="hidden" name="required" id="required" class="required" />
</form>
PHP Code
<?php
/*
* Template Name: Contact
*/
if( isset( $_POST['submitted'] ) ) {
$to = get_option( 'admin_email' );
if( trim( $_POST['name'] ) === '' ) {
$nameError = 'You did not enter your name.';
$hasError = true;
} else {
$name = trim( $_POST['name'] );
}
if( trim( $_POST['email'] ) === '' ) {
$emailError = 'You did not enter your email address.';
$hasError = true;
} else if( !preg_match( "/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+.[a-z]{2,4}$/i", trim( $_POST['email'] ) ) ) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim( $_POST['email'] );
}
if( trim( $_POST['phone'] ) === '' ) {
$phoneError = 'You did not enter your telephone number.';
$hasError = true;
} else if( !is_numeric( $_POST['phone'] ) ) {
$phoneError = 'You have entered an invalid phone number.';
$hasError = true;
} else {
$phone = trim( $_POST['phone'] );
}
if( !empty( $_POST['required'] ) ) {
$requiredError = 'You appear to be a robot. If you are human, please try again.';
$hasError = true;
}
if( !isset( $hasError ) ) {
$subject = 'Call Back Request from ' . $name;
$body = "Name: $name nnEmail: $email nnPhone: $phone";
$headers = 'From: ' . $name . ' <' . $emailTo . '>' . "rn" . 'Reply-To: ' . $email;
wp_mail( $to, $subject, $body, $headers );
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php if( isset( $emailSent ) && $emailSent == true ) { ?>
<p><strong>Your message has been sent successfully. Someone will be in touch shortly. Thanks.</strong></p>
<?php the_content(); ?>
<?php } else { ?>
<?php if( isset( $hasError ) ) { ?>
<p class="error">Sorry, an error has occured.<p>
<ul>
<?php if( $nameError != '' ) { ?>
<li>
<span class="error"><?php echo $nameError; ?></span>
</li>
<?php } ?>
<?php if( $emailError != '' ) { ?>
<li>
<span class="error"><?php echo $emailError; ?></span>
</li>
<?php } ?>
<?php if( $phoneError != '' ) { ?>
<li>
<span class="error"><?php echo $phoneError; ?></span>
</li>
<?php } ?>
<?php if( !empty( $requiredError ) ) { ?>
<li>
<span class="error"><?php echo $requiredError; ?></span>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php the_content(); ?>
<?php } ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
OK Guys,
Looks like I’ve finally figured out the problem. All the code, in all it’s variations was fine and works, except for one tiny thing. The variable ‘$name’ is used by the WordPress system to display the blog name, and as I was using that as my variable for the name field on my form it was causing issues. Who knew.
Anyway, I simply changed it to use ‘$fullName’ and changed the id and name attributes on the form to ‘full-name’ and everything worked great.
Thanks to @maiorano84 for pointing me in the right direction to debug the problem.
Anyway, below is my amended code, so others can make use of it.
Form Code
PHP Code
Tell me if is arrived an email, if not change
$to = get_option( ‘admin_email’ );
with
$to = your@email.com; //exemple
When you have changed try and tell me if it works 😉