Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How to pass error validation message from php to html form

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How to pass error validation message from php to html form

Postby barrington » Wed Jun 12, 2013 4:29 pm

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


<?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&amp;msa=0&amp;msid=208919338781014332881.0004d87a2770823c7c095&amp;t=m&amp;z=17&amp;output=embed"></iframe><br />
<small><a href="https://maps.google.com/maps/ms?ie=UTF8&amp;msa=0&amp;msid=208919338781014332881.0004d87a2770823c7c095&amp;t=m&amp;z=17&amp;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>
barrington
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Mar 09, 2010 7:27 am

Re: How to pass error validation message from php to html fo

Postby throwcode » Wed Jun 12, 2013 7:44 pm

Use a session variable and the controller file as shown here: http://sitepop.wordpress.com/2012/04/12 ... ut-errors/

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

Re: How to pass error validation message from php to html fo

Postby Strider64 » 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.
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 44
Joined: Sat Mar 23, 2013 8:24 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.