Making Pop Ups

I have code where a div pop ups after a successful email is sent.  However, I am failing to get the same effect for when a field isn't filled out. example. Name, Email or messgae.  How would I do this?

<?php
error_reporting(E_ALL);
if(isset($_POST['email'])) {
	// EDIT THE 2 LINES BELOW AS REQUIRED
     $email_to = "ds@schuremediagroup.com";
	 $email_subject = "SMG Contact";
	 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['comments'])) {
		         died('We are sorry, but there appears to be a problem with the form you submitted.');           }         			
				 $subject = $_POST['subject'];
				 $name = $_POST['name']; // required
				 $email_from = $_POST['email']; // required
				 $comments = $_POST['comments']; // required
				 $error_message = "";
				 $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
				 if(!preg_match($email_exp,$email_from)) {
					 $error_message .= 'The Email Address you entered does not appear to be valid.<br />';  }
					 $string_exp = "/^[A-Za-z .'-]+$/";  if(!preg_match($string_exp,$name)) {
						    }
							 if(strlen($comments) < 2) {
								 $error_message .= 'The Comments you entered do not appear to be valid.<br />';  }
								 if(strlen($error_message) > 0) {
									 died($error_message);  }
									 $email_message = "Information.\n\n";
									 function clean_string($string) {
										 $bad = array("content-type","bcc:","to:","cc:","href");
										 return str_replace($bad,"",$string);    }
										 $email_message .= "Subject: ".clean_string($subject)."\n";
										 $email_message .= "Name: ".clean_string($name)."\n";
										 $email_message .= "Email: ".clean_string($email_from)."\n";
										 $email_message .= "Comments: ".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);?>
										   	<div class="popup" "visibility:hidden">
         										<div class="close_popup" align="right">X</div>
            									<div align="center">
               										Request Sent. Thank you!
               									</div>
        									</div>
                                         <?php
                                         }
										 ?>

Open in new window

(edited to add jQuery Zone ~Ray)
DS928Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DS928Author Commented:
I tried this but not working.
<?php
error_reporting(E_ALL);
if(isset($_POST['email'])) {
	// EDIT THE 2 LINES BELOW AS REQUIRED
     $email_to = "ds@schuremediagroup.com";
	 $email_subject = "SMG Contact";
	 function died($error) {
     // your error code can go here
     <div class="popup" "visibility:hidden">
     <div class="close_popup" align="right">X</div>
     	<div align="center">
	 "We are very sorry, but there were error(s) found with the form you submitted. ";
	 	</div>
	 </div>
	 echo "These errors appear below.<br /><br />";
	 echo $error."<br /><br />";
	 echo "Please go back and fix these errors.<br /><br />";
	 die();    }

Open in new window

0
DS928Author Commented:
And this....Not working.

<?php
error_reporting(E_ALL);
if(isset($_POST['email'])) {
	// EDIT THE 2 LINES BELOW AS REQUIRED
     $email_to = "ds@schuremediagroup.com";
	 $email_subject = "SMG Contact";
	 function died($error) {
     // your error code can go here
	 echo "<div>";
   	 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();    
	 echo "<div>";
	 }

Open in new window

0
Ray PaseurCommented:
I added the jQuery Zone to this question because I think you will get a smoother-looking app if you use a modal window for this.  Make a Google search for the exact phrase
jquery modal window email form
and read the first few links.  This one is particularly on-point and would provide a good starting point:
http://jenniferperrin.com/blog/jquery-tutorial-popup-modal-contact-form/

This looks well thought-out, too:
http://net.tutsplus.com/tutorials/javascript-ajax/progressively-enhance-a-form-link-to-modal-form/
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Eddie ShipmanAll-around developerCommented:
Do you think you could post the more code? Well, actually it would be better to build a demo on jsfiddle.net and then let us work with it.
0
DS928Author Commented:
The two working parts are the PHP at the top of the page.  And the form somewhere in the middle.  At the bottom of the PHP code you can see the div that works.  "Request Sent. Thank You"  I want the error code to work the same way.  Thak you.
index.php
0
Slick812Commented:
greetings DS928, I looked at your code in the   index.php   and you do not seem to understand what the top PHP code for the FORM submission seems to do, especially for the "ERRORS" if the form inputs are blank, or the email addy is mal-formed, if the tests find an error this code does the error -
if(strlen($error_message) > 0) { died($error_message);  }

you have a function called died( ) and the last line of it is - die(); - which STOPS all PHP activity and ends the page.

if there is no error then you have this code working -
mail($email_to, $email_subject, $email_message, $headers);
?>
<div class="popup" "visibility:hidden">
<div class="close_popup" align="right">X</div>
<div align="center">
Request Sent. Thank you!
</div>
</div>
<?php  }  ?>

which produces the POP-UP DIV

I really think that this PHP code is in the WRONG PLACE, in my opinion it should be BELOW the <body> tag, because it writes HTML for the div above the <!DOCTYPE  which is entirely incorrect, although it will work most of the time.

To have the ERROR output also in the Pop-UP, you will need to take out (erase) the function died( ), and redo the ERROR catching tests, maybe something like -
<?php
error_reporting(E_ALL);
if(isset($_POST['email'])) {
$subject = $_POST['subject'];
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$popOut = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$popOut .= 'The Email Address entered does not appear to be valid.<br />';  }
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$name)) {
$popOut .= 'The Name entered can Only have Letters no punctuation.<br />';}
if(strlen($comments) < 2) {
$popOut .= 'The Comments need to be at least 3 charaters.<br />';  }
if(strlen($popOut) < 1) {
function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string); }

$popOut = "Request Sent. Thank you!";

$email_message = "Information.\n\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".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);
}
?>
<div class="popup" "visibility:hidden">
<div class="close_popup" align="right">X</div>
<div align="center">
<?php echo $popOut; ?>
</div>
</div>
<?php } ?>

Open in new window


This code is UNTESTED since I can not set this page up on My server, however it should give you an Idea of some ways to go about this, remember to place this code BELOW the <body> tag
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DS928Author Commented:
Thank you!  You nailed this one right on the head!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.