0

Hi All,

(EDIT: Amazingly, I clicked on another thread to give myself a break from coding and i found half the answer immediately. Thanks Pritaeaus http://www.daniweb.com/forums/post1478160.html#post1478160. However, when validation is not passed I still get the messed up page so if anyone can help, I'd be most grateful.)

(EDIT 2: I am certain that this is linked to the die() function used within the died() function)

I have a HTML and PHP form that is not displaying properly when I submit it. It was working (I could receive emails through it) but when the validation failed I only got a messed up page. Now I've messed it up further (don't know how) and it no longed sends mail and the page is still messed up whether it passes validation or not. Can anyone help me?

<!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 name="XAR Files" content="index_htm_files/xr_files.txt"/>
 <title>Steadfast Design - Contact Us</title>
 <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"/>
 <meta name="Generator" content="Xara HTML filter v.3.1.0.319"/>
 <meta name="keywords" content="steadfast design,steadfastdesign,steadfast web design,web design,web site,website,web sites,websites,web designer, website designer, web site designer,webpage, web page, webpage, contact, contact us"/>
 <meta name="description" content="Steadfast Design - Contact Us"/>
 <link rel="stylesheet" type="text/css" href="index_htm_files/xr_main.css"/>
 <link rel="stylesheet" type="text/css" href="index_htm_files/xr_text.css"/>
 <script type="text/javascript" src="index_htm_files/roe.js"></script>
</head>
<body style="background-color: #282D33">
<div class="xr_ap" id="xr_xr" style="width: 800px; height: 600px; top:0px; left:50%; margin-left: -400px;">
 <script type="text/javascript">var xr_xr=document.getElementById("xr_xr")</script>
<!--[if IE]><div class="xr_ap" id="xr_xri" style="width: 800px; height: 600px; clip: rect(0px 800px 600px 0px);"><![endif]-->
<!--[if !IE]>--><div class="xr_ap" id="xr_xri" style="width: 800px; height: 600px; clip: rect(0px, 800px, 600px, 0px);"><!--<![endif]-->
 <img class="xr_ap" src="index_htm_files/0.png" alt="" title="" style="left: 240px; top: 215px; width: 545px; height: 348px;"/>
 <img class="xr_ap" src="index_htm_files/1.png" alt="" title="" style="left: 240px; top: 211px; width: 1px; height: 348px;"/>
 <img class="xr_ap" src="index_htm_files/107.png" alt="" id="Repeating:58MonotoneFooter" title="" style="left: 15px; top: 553px; width: 770px; height: 43px;"/>
 <span class="xr_s0" style="position: absolute; left:15px; top:325px;">
  <span class="xr_tl" style="top: -11px;"><span class="xr_s1">Services</span></span>
  <span class="xr_tl" style="top: 4px;">Steadfast Design is pleased to offer </span>
  <span class="xr_tl" style="top: 20px;">high quality web design services to our </span>
  <span class="xr_tl" style="top: 35px;">clients</span>
 </span>
 <img class="xr_ap" src="index_htm_files/108.png" alt="" title="" style="left: 15px; top: 305px; width: 191px; height: 1px;"/>
 <span class="xr_s2" style="position: absolute; left:262px; top:246px;">
  <span class="xr_tl" style="top: -14px;">Steadfast Design - Contact Us</span>
    <span class="xr_tl xr_s4">
<?php
    if(isset($_POST['email'])) {
	    $email_to = "EMAIL ADDRESS REMOVED";
	    $email_subject = "You have received a message from the contact page of SITE NAME REMOVED";


	function died($error) {
		echo "There is an error(s).  Click the Return button and rectify the error(s) before continuing.<br />";
		echo "The error(s) appears below.<br /><br />";
		echo $error."<br /><br />";
?>

        <input type="image" src="index_htm_files/144.png" name="image" width="94" height="23" onClick="history.go(-1)">

<?php
		die();
	}

	if(!isset($_POST['name']) ||
		!isset($_POST['email']) ||
		!isset($_POST['message'])) {
		died();
	}

	if(isset($_POST['confirm_email'])) {
	      exit();
    }

	$name = $_POST['name']; // required
	$email_from = $_POST['email']; // required
	$message = $_POST['message']; // required

	$error_message = "";
	$email_validate = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
  if(!preg_match($email_validate,$email_from)) {
  	$error_message .= 'The email address is invalid.<br /><br />';
  }
	$string_validate = "/^[a-z .'-]+$/i";
  if(!preg_match($string_validate,$name)) {
  	$error_message .= "The name is invalid.<br /><br />";
  }
  if(strlen($message) < 2) {
  	$error_message .= "The message is invalid.<br />";
  }
  if(strlen($error_message) > 0) {
  	died($error_message);
  }

	$email_message = "Name: ".$name."\n";
	$email_message .= "Email: ".$email_from."\n";
	$email_message .= "Message: ".$message."\n";

	mail($email_to, $email_subject, $email_message);
	echo "Thank you, your message has been sent.  We will reply to you as soon as we can.";
  }
?>
   </span>
 </span>
 <a href="services.htm" onclick="return(xr_nn());">
  <img class="xr_ap" src="index_htm_files/127.png" alt="" title="" onmousemove="xr_mo(this,0,event)" style="left: 15px; top: 381px; width: 62px; height: 23px;"/>
 </a>
 <span class="xr_s5" style="position: absolute; left:32px; top:26px;">
  <span class="xr_tl" style="top: -14px;">Welcome</span>
 </span>
 <img class="xr_ap" src="index_htm_files/128.png" alt="" id="sdbanner" title="" style="left: 15px; top: 38px; width: 770px; height: 177px;"/>
 <span class="xr_s0" style="position: absolute; left:32px; top:183px;">
  <span class="xr_tl" style="top: -11px;">&nbsp;&nbsp; Quality Web Design is our Trademark</span>
 </span>
 <a href="index.htm" target="_self" onclick="return(xr_nn());">
  <img class="xr_ap" src="index_htm_files/8.png" alt="" title="" onmousemove="xr_mo(this,0,event)" style="left: 15px; top: 37px; width: 49px; height: 30px;"/>
 </a>
 <a href="services.htm" target="_self" onclick="return(xr_nn());">
  <img class="xr_ap" src="index_htm_files/9.png" alt="" title="" onmousemove="xr_mo(this,0,event)" style="left: 63px; top: 37px; width: 63px; height: 30px;"/>
 </a>
 <img class="xr_ap" src="index_htm_files/139.png" alt="" title="" style="left: 125px; top: 37px; width: 59px; height: 30px;"/>
 <a href="clients.htm" target="_self" onclick="return(xr_nn());">
  <img class="xr_ap" src="index_htm_files/140.png" alt="" title="" onmousemove="xr_mo(this,0,event)" style="left: 184px; top: 37px; width: 54px; height: 30px;"/>
 </a>
 <img class="xr_ap" src="index_htm_files/12.png" alt="" title="" style="left: 72px; top: 58px; width: 126px; height: 126px;"/>
 <img class="xr_ap" src="index_htm_files/136.png" alt="" title="" style="left: 15px; top: 412px; width: 191px; height: 2px;"/>
 <span class="xr_s0" style="position: absolute; left:15px; top:433px;">
  <span class="xr_tl" style="top: -11px;"><span class="xr_s1">Clients</span></span>
  <span class="xr_tl" style="top: 4px;">Steadfast Design is proud to have </span>
  <span class="xr_tl" style="top: 20px;">created high quality websites for our </span>
  <span class="xr_tl" style="top: 35px;">clients.&nbsp; Please click to view some </span>
  <span class="xr_tl" style="top: 50px;">examples of our work</span>
 </span>
 <a href="clients.htm" onclick="return(xr_nn());">
  <img class="xr_ap" src="index_htm_files/114.png" alt="" title="" onmousemove="xr_mo(this,0,event)" style="left: 15px; top: 505px; width: 54px; height: 23px;"/>
 </a>
 <a href="index.htm" onclick="return(xr_nn());">
  <img class="xr_ap" src="index_htm_files/120.png" alt="" title="" onmousemove="xr_mo(this,0,event)" style="left: 15px; top: 274px; width: 60px; height: 23px;"/>
 </a>
 <span class="xr_s0" style="position: absolute; left:15px; top:234px;">
  <span class="xr_tl" style="top: -11px;"><span class="xr_s1">Home Page</span></span>
  <span class="xr_tl" style="top: 4px;">Click here to return to the main page of </span>
  <span class="xr_tl" style="top: 20px;">Steadfast Design</span>
 </span>
 <img class="xr_ap" src="index_htm_files/141.png" alt="" title="" style="left: 125px; top: 37px; width: 59px; height: 30px;"/>
 <div id="xr_xo0" class="xr_ap" style="left:0; top:0; width:800px; height:100px; visibility:hidden;">
 <a href="" onclick="return(false);">
 <img class="xr_ap" src="index_htm_files/130.png" alt="" title="" style="left: 15px; top: 381px; width: 62px; height: 23px;"/>
 <img class="xr_ap" src="index_htm_files/15.png" alt="" title="" style="left: 15px; top: 37px; width: 49px; height: 30px;"/>
 <img class="xr_ap" src="index_htm_files/16.png" alt="" title="" style="left: 63px; top: 37px; width: 63px; height: 30px;"/>
 <img class="xr_ap" src="index_htm_files/141.png" alt="" title="" style="left: 125px; top: 37px; width: 59px; height: 30px;"/>
 <img class="xr_ap" src="index_htm_files/142.png" alt="" title="" style="left: 184px; top: 37px; width: 54px; height: 30px;"/>
 <img class="xr_ap" src="index_htm_files/119.png" alt="" title="" style="left: 15px; top: 505px; width: 54px; height: 23px;"/>
 <img class="xr_ap" src="index_htm_files/121.png" alt="" title="" style="left: 15px; top: 274px; width: 60px; height: 23px;"/>
 </a>
 </div>
 <div id="xr_xd0"></div>
</div>
</div>
<!--[if lt IE 7]><script type="text/javascript" src="index_htm_files/png.js"></script><![endif]-->
<script type="text/javascript">xr_aeh()</script>
</body>
</html>

Thanks,

Roy

Edited by Roybut: Added comment

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Roybut
2

You are correct. The reason you see the page get chopped up is because anytime you use die() or exit() it does just that and it stops processing the rest of the page. The only time I use those is when it is a critical system failure and an email form really doesn't warrant that in my opinion.

What you want to do with your code is use it like a check list and sort of transcend down through the steps, that is the logic I use on my email form and other types of functions etc.

Now I didn't "fix" your code really but I used what you had and changed a few things here and there, moved some stuff around to make it cleaner, and added more flow to it. I was not totally clear on why you were checking to see if confirm_email had been posted and then you were exiting out if it, but I left it in there anyways.

It really wasn't possible to test this code right now but take a look at it, try it out, and if you get an error just tell me what it was.

<?php
if(!isset($_POST['email'])) {

	// I always tend to set my variables in one place to keep it organized unless I am not able to help it.
	$email_to = "your_email";
	$email_subject = "You have received a message from the contact page of SITE NAME REMOVED";
	
	$name = $_POST['name']; // required
	$email_from = $_POST['email']; // required
	$message = $_POST['message']; // required
	
	$string_validate = "/^[a-z .'-]+$/i";
	$email_validate = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i";


		function died($error) {
			echo "There is an error(s).  Click the Return button and rectify the error(s) before continuing.<br />";
			echo "The error(s) appears below.<br /><br />";
			echo $error."<br /><br />";
			echo '<input type="image" src="index_htm_files/144.png" name="image" width="94" height="23" onClick="history.go(-1)">';
		}
		
		// start our error checking below
		if (empty($name) || empty($email_from) || empty($message)) { //check for empty form fields
			$error = "Please fill in all form fields.<br /><br />";
		}
		
		elseif(!preg_match($email_validate,$email_from)) {
			$error = "The email address is invalid.<br /><br />";
		}
		
		elseif(isset($_POST['confirm_email'])) { // what exactly is this supposed to be doing? It would cause an error if confirm_email is posted??
			$error = "some type of error message";
		}
		
		elseif(!preg_match($string_validate,$name)) {
			$error = "The name is invalid.<br /><br />";
		}
		
		elseif(strlen($message) < 2) {
			$error = "The message is invalid.<br />";
		}
		
		if (!isset($error))  { // if all is well and there is no error then send the email

			$email_message = "Name: ".$name."\n";
			$email_message .= "Email: ".$email_from."\n";
			$email_message .= "Message: ".$message."\n";

			mail($email_to, $email_subject, $email_message);
			echo "Thank you, your message has been sent.  We will reply to you as soon as we can.";
			
		} else {
			died($error); // rather than sending the email show an error to the user if the error variable was present
		}
}
?>

Edited by pixelsoul: n/a

Votes + Comments
Thank you, Pixiesoul.
0

Something else you might want to consider is checking for email headers in the message of the email. With out doing this someone could possible hijack your form and starting sending emails out and spamming people with it. This would be very bad and could get your domain on a email blacklist and it isn't easy to get it removed from those.

0

Hi Pixelsoul,

Thanks for taking the time to reply. I haven't got time to time to try it out right now but I'll get round to it over the weekend.

I'm not sure what the email headers thing is but I'll google it and have a play around.

The confirm_email is a little trick I read about to stop spambots from sending me emails.

Ccheers,

Roy.

0

Hi again,

I've had another look as I can't sleep and it works a treat. Thank you very much:) There was a ! at the beginning but I took that out.

The confirm_email is changed from checking whether it is set to checking the strlen > 0. It will hopefully do its job now.

Thanks very much for your help.

Roy

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.