• Hi!

    Now I’m making a custom contact form included in my theme. But, can’t send the message. Anybody can help me what’s wrong in my code below? thanks before.

    template-contact.php

    <?php
    	/* Template Name: Contact */
    
    if($_POST[sent]) {
    	$email = wp_mail(get_option("admin_email"),"[".get_option("blogname")."] Message from ".trim($_POST[contactName]),stripslashes(trim($_POST[contactText])),"From: ".trim($_POST[contactName])." <".trim($_POST[contactEmail]).">\r\nReply-To:".trim($_POST[contactEmail]));
    
    }
    ?>
    
    <?php get_header(); ?>
    
    			<!--start .content-->
    			<div class="content">
    
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    				<!--start .block-post-->
    				<div class="block-post">
    					<div class="page-title">
    						<h1><?php the_title(); ?></h1>
    					</div>
    					<div class="clear"></div>
    
    					<div class="single-content">
    						<?php the_content(); ?>
    
    						<div class="contact-form-wrap">
    							<form action="<?php the_permalink(); ?>" method="post" class="contact-form" id="contactForm">
    								<input type="hidden" name="sent" id="sent" value="1" />
    
    								<?php if($email){ ?>
    									<p class="email-sent"><?php _e('Message succesfully sent. I\'ll reply as soon as I can', 'xavier'); ?></p>
    								<?php }  ?>
    
    								<div>
    									<label for="contactName"><?php _e('Name', 'xavier'); ?> <span class="required"><?php _e('(required)', 'xavier'); ?></span></label>
    									<input class="text" type="text" name="contactName" id="contactName" value="<?php echo $_POST[contactName];?>" />
    									<p class="error-msg"><?php _e('Please enter a name.', 'xavier'); ?></p>
    								</div>
    
    								<div>
    									<label for="contactEmail"><?php _e('E-mail', 'xavier'); ?> <span class="required"><?php _e('(required)', 'xavier'); ?></span></label>
    									<input class="text" type="text" name="contactEmail" id="contactEmail" value="<?php echo $_POST[contactEmail];?>" />
    									<p class="error-msg"><?php _e('Please enter a valid e-mail address.', 'xavier'); ?></p>
    								</div>
    
    								<div>
    									<label for="contactText"><?php _e('Your Message', 'xavier'); ?> <span class="required"><?php _e('(required)', 'xavier'); ?></span></label>
    									<textarea name="contactText" id="contactText"><?php echo stripslashes($_POST[contactText]); ?></textarea>
    									<p class="error-msg"><?php _e('Please enter a message.', 'xavier'); ?></p>
    								</div>
    
    								<input type="submit" name="submit" class="submit" value="<?php _e('Send Message', 'xavier'); ?>" />
    							</form>
    						</div>
    					</div>
    				</div>
    				<!--end .block-post-->
    				<?php endwhile; endif; ?>
    
    			</div>
    			<!--end .content-->
    
    			<!--start .sidebar-->
    			<div class="sidebar">
    				<?php get_sidebar(); ?>
    			</div>
    			<!--end .sidebar-->
    
    <?php get_footer(); ?>

    script.js

    jQuery(document).ready(function($) {
    	/////////////////////// Contact form ///////////////////////////////////
    	$('#contactForm').live('submit', function(e) {
    		var form = $(this);
    		var name = $(this).find('[name=contactName]').val();
    		var email = $(this).find('[name=contactEmail]').val();
    		var message = $(this).find('[name=contactText]').val();
    
    		if(name == '') {
    			$(this).find('[name=contactName]').addClass('error');
    			$(this).find('[name=contactName]').parent().find('.error-msg').fadeIn();
    
    			return false;
    		} else {
    			$(this).find('[name=contactName]').removeClass('error');
    			$(this).find('[name=contactName]').parent().find('.error-msg').fadeOut();
    		}
    
    		var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    		if(email == ''  || !email_regex.test(email)) {
    			$(this).find('[name=contactEmail]').addClass('error');
    			$(this).find('[name=contactEmail]').parent().find('.error-msg').fadeIn();
    
    			return false;
    		} else {
    			$(this).find('[name=contactEmail]').removeClass('error');
    			$(this).find('[name=contactEmail]').parent().find('.error-msg').fadeOut();
    		}
    
    		if(message == '') {
    			$(this).find('[name=contactText]').addClass('error');
    			$(this).find('[name=contactText]').parent().find('.error-msg').fadeIn();
    
    			return false;
    		} else {
    			$(this).find('[name=contactText]').removeClass('error');
    			$(this).find('[name=contactText]').parent().find('.error-msg').fadeOut();
    		}
    
    		$.ajax({
    			url: XavierVars.ajaxUrl,
    			data: jQuery(form).serialize()+'&action=xavier_contact_form',
    			type: 'POST',
    			success: function() {
    				$('.email-sent').fadeIn(400).delay(5000).fadeOut(400);
    			}
    		});
    
    		e.preventDefault();
    	});
    
    });

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Custom Contact Form Included in Theme’ is closed to new replies.