Page 1 of 1

How do I get the error validation msgs to post top of form

Posted: Wed Jun 12, 2013 11:21 pm
by barrington0

Code: Select all

<?php
if($_POST['frmSubmit'] == "Send") {    

	// CHANGE THE TWO LINES BELOW    
	$email_to = "bobby_arrington2005@yahoo.com";         
	$email_subject = "Contact Form Submissions";
	
	function died($error) 	{        
	
		// your error code can go here        
		echo "We are very sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";        
		echo $error."<br /><br />";        
		echo "Please go back and fix these errors.<br /><br />";        
		die();    
	}         
	
	// validation expected data exists    
	if(!isset($_POST['name']) ||        
		!isset($_POST['email']) ||        
		!isset($_POST['phone']) ||        
		!isset($_POST['message'])) {        
		died('We are sorry, but there appears to be a problem with the form you submitted.');           
	}         
	
	$full_name = $_POST['name']; // required    
	$email_from = $_POST['email']; // required    
	$telephone = $_POST['phone']; // not required    
	$comments = $_POST['message']; // required         
	
	$error_message = "";    
	$string_exp = '/^[a-zA-Z0-9 ]*$/';
	if(!preg_match($string_exp,$full_name)) {    
		$error_message .= "<li>The Name you entered does not appear to be valid.</li>";  
	}  
	$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';  
	if(!preg_match($email_exp,$email_from)) {    
		$error_message .=  "<li>The Email Address you entered does not appear to be valid.</li>";  
	}    
	$string_exp = "/^[A-Za-z .'-]+$/";  
	if(strlen($comments) < 2) {    
		$error_message .=  "<li>The Message you entered do not appear to be valid.</li><br />";  
	}  
	if(strlen($error_message) > 0) {    
		died($error_message);  
	}    	
	$email_message = "Form details below.\n\n";         
	
	function clean_string($string) {      
		$bad = array("content-type","bcc:","to:","cc:","href");      
		return str_replace($bad,"",$string);    
	}         
	
	$email_message .= "Name: ".clean_string($full_name)."\n";    
	$email_message .= "Email: ".clean_string($email_from)."\n";    
	$email_message .= "Telephone: ".clean_string($telephone)."\n";    
	$email_message .= "Message: ".clean_string($comments)."\n";          
	
	// create email headers 	
	$headers = 'From: '.$email_from."\r\n".
	'Reply-To: '.$email_from."\r\n" .
	'X-Mailer: PHP/' . phpversion();
	@mail($email_to, $email_subject, $email_message, $headers); 
}
	header("Location:index.html"); 
	exit;
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>NuLeafFurniture</title>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
<div id="container">
	<h1>NuLeafFurniture.com</h1>
	<div id="topnav">
	  <div id="top">
        <div id="logo">
            
        </div><!-- end #logo -->
      <ul id="MenuBar1" class="MenuBarHorizontal">
        <li><a href="index.html">Home</a>  </li>
        <li><a href="products.html">Products</a></li>
        <li><a href="contactus.html">Contact Us</a></li>
      </ul>
  	</div> <!-- header -->
				</div><!-- end #top -->
      
    <div id="content-secondary">
    <div id="content-left">
    <div id="maintext">  
	<br />
    <hr />
    <table style="width: 100%">
    	<tbody>
        	<tr>
            	<td>
	            	Monday - Saturday 
                </td>
                <td>
                	10am - 6pm
                </td>
            </tr>
            <tr>
            	<td>
	            	Sunday 
                </td>
                <td>
                	12pm - 6pm
                </td>
            </tr>
        </tbody>
    </table>
    <hr />

    <p>NuLeafFurniture Located at The Mercantile<br />7200 Camp Bowie Blvd.<br />Ft Worth, Texas 76116-7123<br /><iframe width="300" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/ms?ie=UTF8&msa=0&msid=208919338781014332881.0004d87a2770823c7c095&t=m&z=17&output=embed"></iframe><br />
    		<small><a href="https://maps.google.com/maps/ms?ie=UTF8&msa=0&msid=208919338781014332881.0004d87a2770823c7c095&t=m&z=17&source=embed" style="color:#0000FF;text-align:left" target="_blank">View Larger Map</a></small></p> 
    </div><!--end maintext -->
    </div><!-- end content left-->
	<div id="content-right">
	<?php if(isset($_SESSION['Contactus.Error']))
		{
			echo $_SESSION['Contactus.Error'];
			unsert($_SESSION['Contactus.Error'];} ?>
    <div id="form-wrapper">
       <p>
	<span><form action="contact.php" method="post">
	
		<div>
			<label for="name"><span class="required">*</span> Name</label>
			<input type="text" name="name" />
		</div>
		<div>
			<label for="email"><span class="required">*</span> Email</label>
			<input type="text" name="email" />
		</div>
		<div>
			<label for="phone"><span class="required">*</span> Phone</label>
			<input type="text" name="phone" />
		</div>
		<div>
			<label for="message"><span class="required">*</span> Message</label>
			<textarea type="textarea" name="message"></textarea>
		</div>
		<div>
			<label for="frmSubmit">&nbsp;</label>
			<input type="submit" name="frmSubmit" value="Send" name="" />
		</div>
	
	</form>
</span></p>

    </div>
</div><!-- end #content-right -->
	<!-- 
    <div id="float-clear"></div>-->
	</div><!---secondary content-->
	
	<div id="footer">
		&copy;2013&nbsp;&bull; NuLeafFurniture.com  &nbsp;&bull;&nbsp; 7200 Camp Bowie Blvd., Ft Worth, TX 76116-7123 &nbsp;&bull;&nbsp;Telephone:&nbsp;214-727-0563 &nbsp;&bull;&nbsp;
		<a href="mailto:info@NuLeafFurniture.com">info@NuLeafFurniture.com</a>
    </div> 
</div> <!-- container -->
</div> <!-- wrapper -->
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
    </script>
</body>
</html>

Re: How do I get the error validation msgs to post top of fo

Posted: Fri Jun 14, 2013 12:49 am
by Christopher
Instead if calling die($error_message), show $error_message in the HTML above the form. You will need to add an if() to not send the email if $error_message is set.

Re: How do I get the error validation msgs to post top of fo

Posted: Fri Jun 14, 2013 11:45 pm
by Strider64
Or you can use the ternary operator in your HTML <body></body> section:

Code: Select all

<?php echo (isset($error_message)) ? $error_message : '<h3>Contact Form</h3>'; ?>

Re: How do I get the error validation msgs to post top of fo

Posted: Sat Jun 15, 2013 12:01 pm
by Christopher
Strider64 wrote:Or you can use the ternary operator in your HTML <body></body> section:

Code: Select all

<?php echo (isset($error_message)) ? $error_message : '<h3>Contact Form</h3>'; ?>
You should do that too. But I don't think the submitted form will ever get there if there is an error -- due to the die().

Re: How do I get the error validation msgs to post top of fo

Posted: Sun Jun 16, 2013 4:20 pm
by barrington0
Thanks...