Link to home
Start Free TrialLog in
Avatar of tf2012
tf2012

asked on

html contact form submit thank you message

I'd like to either use jquery or javascript to show a div or pop a message when the form has been submitted to the php handler.

I've googled it and the examples seem to be pretty confusing.  Below is my form and my form php code.  Can anyone recommend a current best practice method for showing an acknowledgement that the form has been submitted?

Plus my current way of doing this opens sendForm.php and doesn't return the user to the form page... not sure how to do that either in case someone knows how to do that.  Kind of lost here.

					<form action="sendForm.php" name="leadForm" method="post"  onsubmit="return validateMainForm();">
																	
	
							<div class="inputRow">	
		
								<label for="nameField">Name:</label>
		
								<input maxlength="40" name="nameField" type="text" class="ContactFormTextInput" id="nameField" />
	
							</div><!-- inputRow -->
	
							<div class="inputRow">
		
								<label for="phoneField">Phone:</label>
		
								<input maxlength="40" name="phoneField" type="text" class="rightColumnContactFormTextInput" id="phoneField" />							
		
							</div><!-- inputRow -->
	
							<div class="inputRow">
		
								<label for="emailField">Email:</label>
	
								<input maxlength="100" name="emailField" type="text" class="rightColumnContactFormTextInput" id="emailField" />
	
							</div><!-- inputRow -->
	
							<div class="inputTextArea">
	
								<label for="messageField">Details:</label>
								
									<textarea cols="29" name="messageField" id="messageField"></textarea>
									
							</div><!-- inputRow -->
							
			
						<input name="submit" type="submit" value="Send" class="button" />
							
						<script type="text/javascript">
	
							function validateMainForm() {
							
								//super simple email validator Nov 11, 2011
	
								var nameMainForm=document.forms["leadForm"]["nameField"].value;
		
								var emailMainForm=document.forms["leadForm"]["emailField"].value;
		
								var phoneMainForm=document.forms["leadForm"]["phoneField"].value;
	
								//validate email string
		
								var atpos=emailMainForm.indexOf("@");
		
								var dotpos=emailMainForm.lastIndexOf(".");
		
								var emailFailed="0"; //good email = 0
		
		
								if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailMainForm.length) {											
		
									emailFailed="1";											
		
								}
		
								if ((nameMainForm==null || nameMainForm=="")||(phoneMainForm==null || phoneMainForm=="")) {
		
									alert("Please complete all fields in the form.");
		
									return false;
		
								} else if (emailFailed=="1") {
		
									alert("Please include a valid email address");
									
									return false;
		
								}										
		
							}
		
						</script>
			
				</form><!--#form -->

Open in new window

And here is the sendForm.php code
<?php 
//Email address of person who should get email notification of form submission
$toemail = "mymail@here.com";
//-----------------Start send email script------------------------
//This is where you would put any custom scripting such as using php to send confirmation emails
$fromemail = "mail@mymail.com";
$subject = " Quote Request";
$body = "Name: ".$_POST['nameField']."\r\n"."Email: ".$_POST['emailField']."\r\n"."Phone: ".$_POST['phoneField']."\r\n"."Request Details: ".$_POST['messageField']."\r\n";
$header = "From: " . $fromemail . "\r\n";
$header .= "Reply-To: " . $fromemail . "\r\n";
if (!(mail($toemail,$subject,$body,$header))) {
   echo("
<p>Message delivery failed...</p>
 
");
   echo("from email: $fromemail to email: $toemail");
  }
//-----------------End send email script------------------------

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tf2012
tf2012

ASKER

thanks!