Jump to content

Contact form message issues


conzworth
Go to solution Solved by ginerjm,

Recommended Posts

I've built my website off of a template and have everything working except for the contact form. When I test the form and fill it out, it sends the email through to me except the content doesn't all send. I receive messages that say...

Email = name@email.com
Name = First Last
Phone = 123-456-7890
Message = Email = name@email.com
Name = First Last
Phone = 123-456-7890

To sum it up, instead of a subject line only an email address is shown and more importantly the message section only displays an email.


Below is all of the code I have associated with the form, I was looking for somewhere where message=email but failed to find it. Any help is greatly appreciated.


 

 

 

PHP
 

<?php 
 
 
$send_email_to = "name@email.com";
 
function send_email($name,$email,$phone,$subject,$message)
{
  global $send_email_to;  
  if($message=='message')$message='';
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";
  $message = "<strong>Email = </strong>".$email."<br>";  
  $message .= "<strong>Name = </strong>".$name."<br>";     
  $message .= "<strong>Phone = </strong>".$phone."<br>";     
  $message .= "<strong>Message = </strong>".$message."<br>";
  @mail($send_email_to, $subject, $message,$headers);
  return true;
}
 
function validate($name,$email,$phone,$message,$subject)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';  
  $return_array['email_msg'] = '';
  $return_array['phone_msg'] = '';
  $return_array['message_msg'] = '';
  $return_array['subject_msg'] = '';
  
 if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'email is required';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'Enter valid email.';  
    }
  }
 
  if($name == '')
  {
    $return_array['success'] = '0';
    $return_array['name_msg'] = 'Name is required';
  }
  else
  {
     $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $name)) {
      $return_array['success'] = '0';
     $return_array['name_msg'] = 'Enter valid Name.';
    }
  }
  
 
 
  if($subject == '')
  {
    $return_array['success'] = '0';
    $return_array['subject_msg'] = 'Subject is required';
  }
  
  if($message == '')
  {
    $return_array['success'] = '0';
    $return_array['message_msg'] = 'Message is required';
  }
  else
  {
    if (strlen($message) < 2) {
      $return_array['success'] = '0';
      $return_array['message_msg'] = 'Enter valid Message.';
    }
  }  
  
  return $return_array;
}
 
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
 
$return_array = validate($name,$email,$phone,$message,$subject);
if($return_array['success'] == '1')
{
  send_email($name,$email,$phone,$subject,$message);
}
 
header('Content-type: text/json');
echo json_encode($return_array);
die();
 
?>
 

 

HTML

<article class="grid_12">

            <h2>Contact Me</h2>
            <p>Use the form to send me an email and I should get back to you within 24 hours. <br> Or if you <i>really</i> need to talk to me give me a call, I'd love to hear from you. My number's listed below.<br><br></p>
            <form action="#" method="post" id="cform" name="cform">
              <ul id="homehireus" class="hireform contactform">
                <li>
                  <label>Name:<span class="required">*</span></label>
                  <input name="name" id="name" type="text" value="" tabindex="1">
                </li>
                <li>
                  <label>Email:<span class="required">*</span></label>
                  <input name="email" id="email" type="text" value="" tabindex="2">
                </li>
               
               
                <li>
                  <label>Subject:<span class="required">*</span></label>
                  <input name="subject" id="subject" type="text" value="" tabindex="4">
                </li>
                <li>
                  <label>Phone:<span class="required">*</span></label>
                  <input name="phone" id="phone" type="text" value="" tabindex="3" placeholder="867-5309" >
                </li>
               
                <li>
                  <label>Message:<span class="required">*</span></label>
                  <textarea name="message" id="message" value="" tabindex="5"></textarea>
                </li>
                 <li>
                  <input type="button" id="send-message" value="Tell Me What You're Thinking" tabindex="6">
                  <div id="output" class="contactpage-msg"></div>
                </li>
              </ul>
            </form>
          </article>
 
 

Java
 

$(document).ready(function () {
    $('div#output').hide();
    //bind send message here
    $('#send-message').click(sendMessage);
    $('button.close').live('click', function () {
        $(this).parent().find('p').html('');
        $(this).parent().hide();
    });
});
 
/* Contact Form */
function checkEmail(email) {
    var check = /^[\w\.\+-]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/;
    if (!check.test(email)) {
        return false;
    }
    return true;
}
 
function sendMessage() {
    
    // receive the provided data
    var name = $("input#name").val();
    var email = $("input#email").val();
    var subject = $("input#subject").val();
    var phone = $("input#phone").val();
    var message = $("textarea#message").val();
 
    message = 'message';
    // check if all the fields are filled
    if (name == '' || phone == '' || email == '' || subject == '' || message == '') {
        $("div#output").show().html('<button type="button" class="close" data-dismiss="alert-close">x</button><p class="alert-close">You must enter all the fields!</p>');
 
        return false;
    }
 
    // verify the email address
    if (!checkEmail(email)) {
        $("div#output").show().html('<button type="button" class="close" data-dismiss="alert">x</button><p>Please enter a valid Email Address</p>');
        return false;
    }
 
    // make the AJAX request
    var dataString = $('#cform').serialize();
    $.ajax({
        type: "POST",
        url: 'contact.php',
        data: dataString,
        dataType: 'json',
        success: function (data) {
            if (data.success == 0) {
                var errors = '<ul><li>';
                if (data.name_msg != '')
                    errors += data.name_msg + '</li>';
                if (data.email_msg != '')
                    errors += '<li>' + data.email_msg + '</li>';
                if (data.phone_msg != '')
                    errors += '<li>' + data.phone_msg + '</li>';
                if (data.message_msg != '')
                    errors += '<li>' + data.message_msg + '</li>';
                if (data.subject_msg != '')
                    errors += '<li>' + data.subject_msg + '</li>';
 
                $("div#output").removeClass('alert-success').addClass('alert-error').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p> Could not complete your request. See the errors below!</p>' + errors);
            }
            else if (data.success == 1) {
 
                $("div#output").removeClass('alert-error').addClass('alert-success').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p>You message has been sent successfully!</p>');
            }
 
        },
        error: function (error) {
            $("div#output").removeClass('alert-success').addClass('alert-error').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p> Could not complete your request. See the error below!</p>' + error.statusText);
        }
    });
 
    return false;
}

 

Link to comment
Share on other sites

In the very beginning of your code you are wiping out $message.  Fix that.

I'm new to PHP but I believe you're talking about here

 

 

 

function send_email($name,$email,$phone,$subject,$message)
{
  global $send_email_to;  
  if($message=='message')$message='';
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";
  $message = "<strong>Email = </strong>".$email."<br>";  
  $message .= "<strong>Name = </strong>".$name."<br>";     
  $message .= "<strong>Phone = </strong>".$phone."<br>";     
  $message .= "<strong>Message = </strong>".$message."<br>";
  @mail($send_email_to, $subject, $message,$headers);
  return true;
}

 

Could you elaborate on where I'm wiping out $message because I'm not too certain. I tried removing  the second $message=''   from    if($message=='message')$message=''; without a successful change in the outcome but as I said I'm new to this and not too certain if thats what you're talking about.

 

Thanks 

Link to comment
Share on other sites

  • Solution

No. 

 

Did you write this code?  Your problem is here:

  $headers .= "From: ".$email. "\r\n";
  $message = "<strong>Email = </strong>".$email."<br>";  
  $message .= "<strong>Name = </strong>".$name."<br>";     
  $message .= "<strong>Phone = </strong>".$phone."<br>";     
  $message .= "<strong>Message = </strong>".$message."<br>";
  @mail($send_email_to, $subject, $message,$headers);

You begin by putting $mail into $message and continue putting other values in the same var (not a problem) but then you end by trying to put $message into $message, thinking that there is an actual message being stored.

Link to comment
Share on other sites

I definitely haven't fixed it the proper way because the way the message is coming into my email is formatted rather strange but all of the information is now coming through and thats all I really care about. Thanks for the help.
 

{
  global $send_email_to;  
  if($message=='message');
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";
  $email = "<strong>Email = </strong>".$email."<br>";  
  $name .= "<strong>Name = </strong>".$name."<br>";     
  $phone .= "<strong>Phone = </strong>".$phone."<br>";     
  $message .= "<strong>Message = </strong>".$subject  .$name .$email .$phone  .$message."<br>";
  @mail($send_email_to, $subject, $message,$headers);
  return true;
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.