Need help with contact form

Hello,

I’ve tried creating a contact form using the following:

<form name="contact_form" method="post" action="">      
    <p>
      <label for="name" class="fixedwidth">Your Name: </label>
      <input name="name" type="text" id="name" size="40" />
      <label for="name" id="name_error" class="error"><em>*</em></label>
    </p>
    <p>
      <label for="email" class="fixedwidth">Your Email: </label>
      <input name="email" type="text" id="email" size="40" />
      <label for="email" id="email_error" class="error"><em>*</em></label>
    </p>
    <p>
      <label for="subject" class="fixedwidth">Subject: </label>
      <input name="subject" type="text" id="subject" size="40" />
      <label for="subject" id="subject_error" class="error"><em>*</em></label>
    </p>
    <p>
      <label for="msg" class="fixedwidth">Your Message: </label>
      <textarea name="msg" id="msg" cols="40" rows="7"></textarea>
      <label for="msg" id="msg_error" class="error"><em>*</em></label>
    </p>
    <div id="button">
      <input name="submit" type="submit" value="Submit" class="button" />
    </div>
    </form>
$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
  $(".button").click(function() {
		
    $('.error').hide();
	
          var name = $("input#name").val();  
          if (name == "") {  
          $("label#name_error").show();  
          $("input#name").focus();  
          return false;  
        }  
          var email = $("input#email").val();  
          if (email == "") {  
          $("label#email_error").show();  
          $("input#email").focus();  
          return false;  
        }  
          var subject = $("input#subject").val();  
          if (subject == "") {  
          $("label#subject_error").show();  
          $("input#subject").focus();  
          return false;  
        }  
		 var msg = $("textarea#msg").val();  
            if (msg == "") {  
          $("label#msg_error").show();  
          $("textarea#msg").focus();  
          return false;
    }
		var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&msg=' + msg;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "process.php",
      data: dataString,
      success: function() {
        $('#contact').html("<div id='message'></div>");
        $('#message').html("<h2>Your Message Was Submitted!</h2>")
        .append("<p>We will be in touch with you soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
<?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
    $name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
    $email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'Invalid email';}
if ((isset($_POST['subject'])) && (strlen(trim($_POST['subject'])) > 0)) {
    $phone = stripslashes(strip_tags($_POST['subject']));
} else {$phone = 'No Subject';}
if ((isset($_POST['msg'])) && (strlen(trim($_POST['msg'])) > 0)) {
    $phone = stripslashes(strip_tags($_POST['msg']));
} else {$phone = 'No message entered';}
ob_start();
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
  <tr bgcolor="#eeffee">
    <td>Name</td>
    <td><?=$name;?></td>
  </tr>
  <tr bgcolor="#eeeeff">
    <td>Email</td>
    <td><?=$email;?></td>
  </tr>
  <tr bgcolor="#eeffee">
    <td>Subject</td>
    <td><?=$subject;?></td>
  </tr>
  <tr bgcolor="#eeffee">
    <td>Message</td>
    <td><?=$msg;?></td>
  </tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();

$to = 'myemail.com';
$email = 'myemail.com';
$fromaddress = "myemail.com";
$fromname = "Online Contact";

require("/class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "myemail.com";
$mail->FromName = "Contact Form";
$mail->AddAddress("email_address@example.com","Name 1");
$mail->AddAddress("another_address@example.com","Name 2");

$mail->WordWrap = 50;
$mail->IsHTML(true);

$mail->Subject  =  "Form:  Contact form submitted";
$mail->Body     =  $body;
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send()) {
    $recipient = 'myemail.com';
    $subject = 'Contact form failed';
    $content = $body;    
  mail($recipient, $subject, $content, "From: [email]mail@yourdomain.com[/email]\\r\
Reply-To: $email\\r\
X-Mailer: DT_formmail");
  exit;
}
?>

Everything works from a users end- meaning the form validates that all fields have been filled in, and the user gets the confirmation message, without the page refreshing, that form was submitted… BUT I do not receive the emails. I’m assuming there must be an issue within the php.

What error(s) have I made???

Also, if you could assist with a better jquery validation, it would be greatly appreciated!!!

Regards,

better contacts forms http://www.emailmeform.com/

Thanks for the reply and suggestion, but my goal is to become a developer and learn the coding.

if(!$mail->Send()) { 

According to that line, you will only get sent an email if PHPMailer FAILS to Send() the email.

If you want to reverse that decision, then remove that exclamation mark (!)

I bet you change your mind back after a while though :wink:

To understand what is going on at that line;

the logic in the if conditional check is as follows:

if( something is true ) {
then do this
}

Which is the same as saying

if( something is false ) {
then NEVER do this
}

So:

if( PHPMailer Send is equal to true ){
send an email to me
}

Which is what you seem to want …

Hence with the ! negation imposed upon it:

if( it is true that PHPMailer Send is false ){
send an email to me
}

So remove the negation (!) and you will have reverted back to :

if( PHPMailer Send is equal to true ){
send an email to me
}

So all the time you are playing with true/false - that is what if() is assessing.

It might be *ugger to get your head round, but is a keystone of any language.

HTH

Thanks Cups! Great explanation which made perfect sense… and it worked!

Thanks again!