0

I have set up a PHP mail form set up that only correctly outputs some of the variables entered in the form. It DOES mail the $name and $email variables, but not the $message variable.

The php to send the form is here:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST" ){


$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
      //sending email
      require_once("siteIncludes/class.phpmailer.php");
      $mail = new PHPMailer();

$email_body = "";
$email_body = $email_body . "Name: " . $name . $message . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message; 

    $mail->SetFrom("$email,$name");
    $address = "[email protected]";
    $mail->AddAddress($address);
    $mail->Subject = "Form Submission | ".$name;
    $mail->MsgHTML($email_body);

      if(!$mail->Send() ){
        echo 'There was a problem sending the email: '.$mail->ErrorInfo;
        exit();
      } 

    header("Location: myContact.php?status=thanks");
    exit();
};

?>

And the HTML that sets up the form is here:

    <div id="contactFormWrap" class="span6 offset3">        
      <form method="post" action="myContact.php" id="contactForm">
          <div>
            <label for="name">Please leave your name</label>
            <input type="text" name="name" id="name" value="" class="required" />
          </div>
          <div>
            <label for="email">and your email</label>
            <input type="text" name="email" id="email" value="" class="required email" />
          </div> 
          <div>
            <label for="subject">What is your message about?</label>
            <input type="text" name="subject" id="subject" value="" class="required" />
          </div> -->

          <div>
            <label for="message">and your message</label>
            <textarea name="message" id="message" value="" rows="10" class="required"></textarea>
          </div>
          <div id="messageButtons">
          <input type="submit" value="Submit" name="contactSubmit" id="contactSubmit" class="sendEmail btn" />

        </div>
      </form>

    </div>

I hope that was enough information. Does anyone know why the $message variable isn't being output to the submitted email?

thanks

2
  • Put this at the top of your PHP file: error_reporting(-1); Commented Aug 18, 2013 at 4:24
  • Not sure if it can cause an error, but a textarea does not have a value attribute. Commented Aug 18, 2013 at 4:26

1 Answer 1

6

I think this is your problem:

value=""

The <textarea> tag does not have a value attribute, however different browsers have different ways of handling invalid code, so whatever browser you are using must be using the value found in this invalid attribute instead of what you type in the actual text box.

Just do:

<textarea name="message" id="message" rows="10" class="required"></textarea>
Sign up to request clarification or add additional context in comments.

3 Comments

What I can't understand is, both name and email have value="" and the OP is receiving those values.
@Fred because <input> tags do have a value attribute which specifies the default value in the input box. When the user alters this, it alters the value of value. "message" is a <textarea>, so it is not supposed to contain this, and the browser is interpreting this bad HTML in a different way. I wouldn't be surprised if you tried it in a few other browsers and got different results.
Aw, nuts. Taking away the value="" from my textarea field didn't resolve the issue. Is there something else we can try?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.