• So for my Custom Theme I’ve created a Custom Contact form and I have error messages but the thing is the error messages are showing even though it’s the person first time on the page. Basically I only want the error message to show when the user has clicked submit and they have missed something.

    Code:

    <?php
    	//Response Generation Function
    	$response = "";
    
    	//Function to Generate Response
    	function my_contact_form_generate_response($type, $message) {
    
    		global $response;
    	}
    
    	//Response Messages
    	$not_human       = "Human verification incorrect.";
    	$missing_content = "Please supply all information.";
    	$email_invalid   = "Email Address Invalid.";
    	$message_unsent  = "Message was not sent. Try Again.";
    	$message_sent    = "Thanks! Your message has been sent.";
    
    	//User Posted Variables
    	$name = $_POST['message_name'];
    	$email = $_POST['message_email'];
    	$message = $_POST['message_text'];
    	$human = $_POST['message_human'];
    
    	//PHP Mailer Variables
    	$to = get_option('admin_email');
    	$subject = "Someone sent a message from ".get_bloginfo('name');
    	$headers = 'From: '. $email . "\r\n" .
    	'Reply-To: ' . $email . "\r\n";
    
    	if(!$human == 0){
    	if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    	else {
    
    	  //validate email
    	  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    	    my_contact_form_generate_response("error", $email_invalid);
    	  else //email is valid
    	  {
    	    //validate presence of name and message
    	    if(empty($name) || empty($message)){
    	      my_contact_form_generate_response("error", $missing_content);
    	    }
    	    else //ready to go!
    	    {
    	      $sent = wp_mail($to, $subject, strip_tags($message), $headers);
    	      if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
    	      else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
    	    }
    	  }
    	}
    	}
    	else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
    ?>
    
    <?php get_header(); ?>
    <!-- Page Header -->
    <section class="page-title">
    	<h1>
    	<?php wp_title(''); ?>
    	<div class="light-separator small center"></div>
    	</h1>
    </section>
    <section class="contact-page custom-container">
    	<h2>Get in touch!</h2>
    	<div class="dark-separator small center"></div>
    	<div id="respond">
    		<?php echo $response; ?>
    		<form action="<?php the_permalink(); ?>" method="post">
    			<div class="contact-form col-md-12">
    				<div class="col-md-6">
    					<br><input type="text" name="message_name" class="form-name" value="<?php echo esc_attr($_POST['message_name']); ?>" placeholder=" Name *">
    					<small class="error"><?php echo ($_POST["message_name"] == "") ? "Please fill in the required field" : ""; ?></small>
    				</div>
    				<div class="col-md-6">
    					<br><input type="text" class="form-email" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>" placeholder=" E-Mail *">
    					<small class="error"><?php echo ($_POST["message_email"] == "") ? "Please fill in the required field" : ""; ?></small>
    				</div>
    				<div class="col-md-6">
    					<br><input type="text" class="form-tele" name="message_tele" value="<?php echo esc_attr($_POST['message_tele']); ?>" placeholder=" Telephone *">
    					<small class="error"><?php echo ($_POST["message_tele"] == "") ? "Please fill in the required field" : ""; ?></small>
    				</div>
    				<div class="col-md-6">
    					<br><input type="text" class="form-subject" name="message_subject" value="<?php echo esc_attr($_POST['message_subject']); ?>" placeholder=" Subject *">
    					<small class="error"><?php echo ($_POST["message_subject"] == "") ? "Please fill in the required field" : ""; ?></small>
    				</div>
    				<div class="col-md-12 col-sm-12 col-xs-12">
    					<br><textarea type="text" class="form-message" name="message_text" placeholder=" Message *"><?php echo esc_textarea($_POST['message_text']); ?></textarea>
    					<small class="error"><?php echo ($_POST["message_text"] == "") ? "Please fill in the required field" : ""; ?></small>
    				</div>
    				<div class="submit-btn col-md-12">
    					<input type="hidden" name="submitted" value="1">
    					<input type="submit" class="form-button" value="Send">
    				</div>
    				<div class="all-errors">
    					<small class="error"><?php echo ($_POST["missing_content"] == "") ? "Validation errors occurred. Please confirm the fields and submit it again." : ""; ?></small>
    				</div>
    			</div>
    		</form>
    	</div>
    </section>
    <?php get_footer(); ?>

    I’ve been copying this tutorial to make a Custom Contact form for WordPress

    Tutorial

The topic ‘[Custom-Theme] WordPress Custom Contact Form’ is closed to new replies.