Classified Posted May 14, 2012 Share Posted May 14, 2012 Hi, i'm trying to use a contact script, and seems like everything else is working fine, but when i test the script i get these 2 messages (At least): Notice: Undefined variable: subject in E:Domains***** \contactfin.php on line 36 Notice: Undefined variable: subject in E:\Domains*****\contactfin.php on line 39 1. <?php 2. //If the form is submitted 3. if(isset($_POST['submit'])) { 4. 5. //Check to make sure that the name field is not empty 6. if(trim($_POST['contactname']) == '') { 7. $hasError = true; 8. } else { 9. $name = trim($_POST['contactname']); 10. } 11. 12. //Check to make sure that the subject field is not empty 13. if(trim($_POST['subject']) == '') { 14. $hasError = true; 15. } else { 16. $subject = trim($_POST['subject']); 17. } 18. 19. //Check to make sure sure that a valid email address is submitted 20. if(trim($_POST['email']) == '') { 21. $hasError = true; 22. } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 23. $hasError = true; 24. } else { 25. $email = trim($_POST['email']); 26. } 27. 28. //Check to make sure comments were entered 29. if(trim($_POST['message']) == '') { 30. $hasError = true; 31. } else { 32. if(function_exists('stripslashes')) { 33. $comments = stripslashes(trim($_POST['message'])); 34. } else { 35. $comments = trim($_POST['message']); 36. } 37. } 38. 39. //If there is no error, send the email 40. if(!isset($hasError)) { 41. $emailTo = 'info@autovisionfinland.com'; //Put your own email address here 42. $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; 43. $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 44. 45. mail($emailTo, $subject, $body, $headers); 46. $emailSent = true; 47. } 48. } 49 ?> the form with divs and everything comes right after that code, i can copy it too if needed. Any help is appriciated -Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/ Share on other sites More sharing options...
Maq Posted May 14, 2012 Share Posted May 14, 2012 You need to check if it's set: if(!isset($_POST['subject']) || trim($_POST['subject']) == '') { Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/#findComment-1345448 Share on other sites More sharing options...
Classified Posted May 15, 2012 Author Share Posted May 15, 2012 sorry, in my site were i added the code i had removed this part //Check to make sure that the subject field is not empty if(trim($_POST['subject']) == '') { $hasError = true; } else { $subject = trim($_POST['subject']); } I removed it because i don't want subject an required field, just optional. So i placed this code above back in the script and now no errors show... How do i make 'subject' optional? I removed the options from the var validator = $("#contactform").validate({ in javascript (jquery.validate.pack.js) And another problem occurred, even tho the script says the message was successfully sent... I'm not receiving them. There are no options in any of the scripts for outgoing smtp or anything, i've tried a plain php contact script which worked fine, and i have activated on host ASP, php5 and SSI. and deactivated Perl Cgi and CGI executables. I'm really sorry, but i don't know much of these things when it goes outside html and basic php Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/#findComment-1345526 Share on other sites More sharing options...
Classified Posted May 15, 2012 Author Share Posted May 15, 2012 Ok, so the problem of not receiving is because of my host it seems, worked perfectly to my gmail account. But if anybody could help me how to make the subject field optional i would be soooo happy Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/#findComment-1345548 Share on other sites More sharing options...
Classified Posted May 15, 2012 Author Share Posted May 15, 2012 if it helps, here is the exact same script i'm using, you can see it better in this page as an whole: https://gist.github.com/2389741 Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/#findComment-1345605 Share on other sites More sharing options...
Maq Posted May 15, 2012 Share Posted May 15, 2012 If it's not set or blank you should give it a default value. Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/#findComment-1345619 Share on other sites More sharing options...
Classified Posted May 15, 2012 Author Share Posted May 15, 2012 i have no idea what a default value is... i googled and found something like this !== null so i made the optional field like this if(trim($_POST['subject']!== null) == '') { $hasError = true; } else { $subject = trim($_POST['subject']); } I don't know if that's right, but it worked Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/262528-notice-undefined-variable-subject-in/#findComment-1345621 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.