mickyginger Posted October 6, 2009 Share Posted October 6, 2009 Hi guys, total noob here... So I've been tinkering around with a html and am using php to email the subitted data to me. It's all working fine, except now I've changed the rules of the form a little and I can't seem to make it do a very simple task of converting field data to a string so that I can have it in the body of the email. Here's the code: <?php //If the form is submitted if(isset($_POST['submit'])) { //Check to make sure that the name field is not empty if(trim($_POST['name']) == '') { $hasError = true; } else { $name = trim($_POST['name']); } //Check to make sure sure that a valid email address is submitted if(trim($_POST['email']) == '') { $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { $hasError = true; } else { $email = trim($_POST['email']); } //If there is no error, send the email if(!isset($hasError)) { $emailTo = '[email protected]'; //Put your own email address here $subject = 'Interest from the website'; $body = $name . $email; $headers = 'From:' .$name. '<'.$email.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?> Which works fine and dandy, only there are other fields like 'date' and 'venue' which I want to add to the body of the email regardless of whether they have been left blank or not. Basically I want it to do something like this: If the form is submitted if name is not empty $name = trim($_POST['name']); if email is not empty $email = trim($_POST['name']); if number is empty OR if not $number = trim($_POST['number']); if date is empty OR if not $date = trim($_POST['date']); if there is no error $body = $name . $email . $number . $date seems like it should be really simple but I've tried everything I can think of with no joy... Thanks is advance guys, sorry if this has come up before, but I'm such a noob, I can find snippets of code that help but it seems to be the if clause or just the grammar of the whole thing that's messing me around. M Link to comment https://forums.phpfreaks.com/topic/176699-trim-function-issues/ Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 Oddly enough your pseudo code is almost exactly how it is. The exclaimation part means not, and they empty() function checks if the string provided is empty. if(!empty($_POST['name'])) { $name = trim($_POST['name']); } Link to comment https://forums.phpfreaks.com/topic/176699-trim-function-issues/#findComment-931600 Share on other sites More sharing options...
mickyginger Posted October 6, 2009 Author Share Posted October 6, 2009 Awesome man, I can't thank you enough. Works a treat. Best, M Link to comment https://forums.phpfreaks.com/topic/176699-trim-function-issues/#findComment-931730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.