How to pass error validation message from php to html form

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 9
Joined: Tue Mar 09, 2010 7:27 am

Wed Jun 12, 2013 4:29 pm

How do I pass error message back and display above the html form....

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

$email_to = "";
$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 />";

// 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) {
$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);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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" />

<div id="wrapper">
<div id="container">
<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>
</div> <!-- header -->
</div><!-- end #top -->

<div id="content-secondary">
<div id="content-left">
<div id="maintext">
<br />
<hr />
<table style="width: 100%">
Monday - Saturday
10am - 6pm
12pm - 6pm
<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=" ... iframe><br />
<small><a href=" ... urce=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">
<span><form action="contact.php" method="post">

<label for="name"><span class="required">*</span> Name</label>
<input type="text" name="name" />
<label for="email"><span class="required">*</span> Email</label>
<input type="text" name="email" />
<label for="phone"><span class="required">*</span> Phone</label>
<input type="text" name="phone" />
<label for="message"><span class="required">*</span> Message</label>
<textarea type="textarea" name="message"></textarea>
<label for="frmSubmit">&nbsp;</label>
<input type="submit" name="frmSubmit" value="Send" name="" />


</div><!-- end #content-right -->
<div id="float-clear"></div>-->
</div><!---secondary content-->

<div id="footer">
&copy;2013&nbsp;&bull; &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=""></a>
</div> <!-- container -->
</div> <!-- wrapper -->
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});

New php-forum User
New php-forum User
Posts: 34
Joined: Mon Sep 05, 2011 7:35 am

Wed Jun 12, 2013 7:44 pm

Use a session variable and the controller file as shown here: ... ut-errors/


User avatar
php-forum Active User
php-forum Active User
Posts: 290
Joined: Sat Mar 23, 2013 8:24 am

Thu Jun 13, 2013 4:16 am

Or you could do this:

Code: Select all

<h2><?php echo (isset($error_message)) ? $error_message : "Contact Page"; ?></h2> 
this is considering you don't redirect your error messages and the HTML view code is either in the same file or is included.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests