• Hi there,

    I’ve been following this tutorial to create my own contact form – template, and it works great, it sends the email. But I do get a weird error in all the fields when loading the page.

    This is the error:
    Notice: Undefined index: message_email in *domain*/wp-content/themes/dobocor/template-contact-2.php on line 100

    So, it has something to do with the PHP of my input fields, but I have no idea what it is. Here is my PHP template:

    <?php
    /**
     * Template Name: Contact Form 2
     *
     */
    ?>
    
    <?php
    
      //response generation function
      $response = "";
    
      //function to generate response
      function my_contact_form_generate_response($type, $message){
    
        global $response;
    
        if($type == "success") $response = "<div class='success'>{$message}</div>";
        else $response = "<div class='error'>{$message}</div>";
    
      }
    	//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: ','dobocor') .$name. '<no-reply@'.$_SERVER['SERVER_NAME'].'>' . "\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(); ?>
    
    <?php
    if (rwmb_meta('wpos_service_button') != '') {
    	$button =  rwmb_meta('wpos_service_button');
    } ?>
    
    <div class="bg-gray home-section">
    <div class="container">
    	<div class="row">
    
    	<div id="primary" class="col-md-12 col-lg-12">
    		<main id="main" class="site-main" role="main">
    			<div class="row">
    
    				<div class="container marginbot-50">
    					<div class="row">
    						<div class="col-lg-8 col-lg-offset-2">
    							<div>
    							<div class="section-heading text-center">
    							<h2 class="h-bold">
    								<?php the_title(); ?>
    								<?php echo $_SERVER['SERVER_NAME']; ?>
    							</h2>
    							<div class="divider-header"></div>
    							</div>
    							</div>
    						</div>
    					</div>
    				</div>
    
    				<div id="respond">
    				  <?php echo $response; ?>
    				  <form action="<?php the_permalink(); ?>" method="post">
    				    <p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
    				    <p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
    				    <p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
    				    <p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
    				    <input type="hidden" name="submitted" value="1">
    				    <p><input type="submit"></p>
    				  </form>
    				</div>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    	</div><!-- .row -->
    </div><!-- .container -->
    </div><!-- .bg-gray -->
    <?php get_footer(); ?>

    can anyone help me?

Viewing 1 replies (of 1 total)
  • the clues are in the message, line 100, Undefined index: “message_email

    <p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>

    in php [ ] is an array and undefined index means just that there is no message_email in the array. basically php is bitching that you’re trying to use something that doesn’t exist!!

    swapping :

    <?php echo esc_attr($_POST['message_email']); ?>

    for :

    <?php echo isset( $_POST['message_email'] ) ? esc_attr($_POST['message_email']) : '';  ?>

    should solve your problem. ps. if that freaks you out its just php short hand for an if else statement
    so long hand

    <?php if( isset( $_POST['message_email'] ) ){
     echo esc_attr($_POST['message_email']);
    }
    else{
     echo '';
    }
    ?>

    it’s good practice to make sure something exists before you try and use it ??

Viewing 1 replies (of 1 total)
  • The topic ‘Notice: Undefined index: Error in contact form’ is closed to new replies.