Jump to content

Comment not working in email


yorkshirekid
Go to solution Solved by yorkshirekid,

Recommended Posts

Hello. I am very new to php. In fact I've used it once! I made a webpage and used some code I got off the net to and the comment part does not work - the rest does. I'd be grateful if somebody would help me fix it please. I want to use this code and not any other code please.  Here it is:

===================================

<?php
    //define variables that are same as Contact Form and give them null values
    $name = "";
    $email = "";
    $comment = "";

 // if the submit button is clicked then please do the following...        
    if (isset($_POST['submit'])) {

// this function 'sanitises' the input data
// gets rid of white space
// changes none html characters to html characters    
// helps prevent potentioal injection by hacker
    function test_input($data) {  
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data; // run the function
    }

// Takes input data and sends it to the function above and then posts the form
    if ($_SERVER["REQUEST_METHOD"] == "POST") {        
    $name = test_input ($_POST["name"]);
    $email = test_input ($_POST["email"]);
    $comment = test_input ($_POST["comment"]);
    }

//  Who is the form going to and what is the Subject
    $to = "fred@bloggs.com";
    $subject = "Website Feedback";
    }
    
// Format how the message will be recieved
    $message ='Name: '.$name.'<br /><br />Email:'.$email.'<br /><br />Comment:'.$comment;

// Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    
    mail($to, $subject,$message,$headers);
        
?>

====================================

Link to comment
Share on other sites

“Doesn't work” isn't sufficient information. What exactly is the problem? Is $_POST["comment"] set at all? What's the content of $comment? Use var_dump() to check this. Are there any error messages in the PHP log?

 

The code in general doesn't make a lot of sense. For example, you check if the client used the POST method (you even do it twice, for some strange reason), but you never actually check if the specific fields are present, and you send the e-mail in any case. If I simply visit the page and reload it a couple of times, you end up with dozens of empty e-mails in your inbox.

 

I assume this is all copied and pasted from different websites? While I can understand that you're a beginner who just wants to get things done, this approach will lead to very bad code and is a dead end. You should learn the language and write your own code. Even if it won't be perfect at first, you'll at least understand what's going on and make progress.

Link to comment
Share on other sites

Ok thanks. The problem is as I outlined - that there is no comment received. Everything else is working - the name and email fields. I can put text in the comment box but it is blank when received. I don't understand the 'code questions' you ask. Yes, I'm reading and trying things from all over the place.

 

I thought I'd give this email form a try and was pleased it went ok except the one part. I am just after a resolve for that part atm. I just thought an expereinced programmer may spot an error, sorry. Thanks for looking.

Edited by yorkshirekid
Link to comment
Share on other sites

  • Solution

Smarty pants :happy-04: . You nailed it Jacques1. Thank you.

Asking me about the text field made me look to see and I had named it Comment instead of comment. That's intuition for you - something I lack; which is why I came to this forum.

Again - many thanks.

I'm going to learn how to use var_dump now.

Cheers

:thumb-up:

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.