yorkshirekid Posted July 18, 2016 Share Posted July 18, 2016 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); ?> ==================================== Quote Link to comment https://forums.phpfreaks.com/topic/301506-comment-not-working-in-email/ Share on other sites More sharing options...
Jacques1 Posted July 19, 2016 Share Posted July 19, 2016 “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. Quote Link to comment https://forums.phpfreaks.com/topic/301506-comment-not-working-in-email/#findComment-1534629 Share on other sites More sharing options...
yorkshirekid Posted July 19, 2016 Author Share Posted July 19, 2016 (edited) 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 July 19, 2016 by yorkshirekid Quote Link to comment https://forums.phpfreaks.com/topic/301506-comment-not-working-in-email/#findComment-1534630 Share on other sites More sharing options...
Jacques1 Posted July 19, 2016 Share Posted July 19, 2016 The code alone doesn't tell us anything. We need concrete information, which is why I'm asking you to insert var_dump() statements into your code and tell us the exact content of $_POST['comment'] and $comment. Are you sure the comment box in the contact form is a text field with the name comment? Quote Link to comment https://forums.phpfreaks.com/topic/301506-comment-not-working-in-email/#findComment-1534632 Share on other sites More sharing options...
Solution yorkshirekid Posted July 19, 2016 Author Solution Share Posted July 19, 2016 Smarty pants . 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 Quote Link to comment https://forums.phpfreaks.com/topic/301506-comment-not-working-in-email/#findComment-1534633 Share on other sites More sharing options...
benanamen Posted July 19, 2016 Share Posted July 19, 2016 Get in the habit of always using lower case for names and variables and seperate_multiple_words_with_an_underscore. Quote Link to comment https://forums.phpfreaks.com/topic/301506-comment-not-working-in-email/#findComment-1534634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.