michaelramsgate Posted January 24, 2011 Share Posted January 24, 2011 First off, I am new to PHP, and want to learn it, but it seems very complicated. Where do I begin? I found a script online for a contact form and I am having trouble making it work for my needs. I wanted to include a phone number field in the email message. This is what I have so far for the PHP, but it does not include the phone field in the body of the message. Everything else works fine. Apologize in advance if this message has been posted many times before. <?php $post = (!empty($_POST)) ? true : false; if($post) { include 'functions.php'; $name = stripslashes($_POST['contactname']); $email = trim($_POST['email']); $subject = "New Message from your website"; $message = stripslashes($_POST['message']); $phone = stripslashes($_POST['phone']); $error = ''; // Check name if(!$name) { $error .= 'Please enter your name.<br />'; } // Check email if(!$email) { $error .= 'Please enter an e-mail address.<br />'; } if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; } // Check message (length) if(!$message || strlen($message) < 15) { $error .= "Please enter your message. It should have at least 15 characters.<br />"; } if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '<div class="notification_error">'.$error.'</div>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/ Share on other sites More sharing options...
coupe-r Posted January 24, 2011 Share Posted January 24, 2011 This is because you are only passing in the $message var in your mail() function. $message only has the contents of $_POST[message] You need to do something like: $message = $_POST['message']; $message .= '/n/n'; $message .= $phone; Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164668 Share on other sites More sharing options...
michaelramsgate Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks for the quick reply....that makes total sense to me now, and sure enough it works. you added $message .= '/n/n'; what exactly is this supposed to do? How would one go about styling it further to say $message= The customer's phone number is =$phone; Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164675 Share on other sites More sharing options...
michaelramsgate Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks for the quick reply....that makes total sense to me now, and sure enough it works. you added $message .= '/n/n'; what exactly is this supposed to do? How would one go about styling it further to say $message= The customer's phone number is =$phone; $message .= "Customer Phone Number is:" .$phone; is there a way to force this its own line of the email. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164704 Share on other sites More sharing options...
BlueSkyIS Posted January 24, 2011 Share Posted January 24, 2011 $message .= '/n/n'; should be $message .= "\n\n"; , which will add 2 line breaks. the code as posted above will cause /n/n to be added to the message. use "\n" for a new line. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164724 Share on other sites More sharing options...
coupe-r Posted January 24, 2011 Share Posted January 24, 2011 \n\n adds 2 line breaks in the email message. \n - New Line Try this. $message = "The customer's phone number is = ".phone; Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164725 Share on other sites More sharing options...
BlueSkyIS Posted January 24, 2011 Share Posted January 24, 2011 /n/n adds 2 line breaks in the email message. /n - New Line No. \n is new line. And placing single quotes around \n causes it to be interpreted literally as \n instead of a new line. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164727 Share on other sites More sharing options...
coupe-r Posted January 24, 2011 Share Posted January 24, 2011 Yes, you are correct, we wrote those messages at the same time and when I went back to look at yours, I already sent the message. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164728 Share on other sites More sharing options...
michaelramsgate Posted January 24, 2011 Author Share Posted January 24, 2011 this has been really great...thanks for the help just one more question adding this $message = stripslashes($_POST['message']); $message .= "\n\n"; $message = "Customer Phone Number is:" .phone; seems to loose the validation for the message. If I dont put anything in the message field, it does not check and sends anyway. // Check message (length) if(!$message || strlen($message) < 15) { $error .= "Please enter your message. It should have at least 15 characters.<br />"; } if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '<div class="notification_error">'.$error.'</div>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1164736 Share on other sites More sharing options...
michaelramsgate Posted January 25, 2011 Author Share Posted January 25, 2011 still need some help, I am not sure how to use the above code and still get the rest of the message validated. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165002 Share on other sites More sharing options...
KevinM1 Posted January 25, 2011 Share Posted January 25, 2011 $message = "Customer Phone Number is:" .phone; Should be: $message .= "Customer Phone Number is: " . $phone; Why? 1. The '$' denotes a variable. Without it, PHP thinks that you have a named constant titled 'phone' that you're trying to add to your $message string. 2. The '.' is the string concatenation operator. It simply adds whatever value is to the right to the string on the left. Think of it like addition. 3. The '=' is the assignment operator. It takes the value on the right of it and assigns it to the variable on the left. 4. '.=' is a shorthand concatenation and assignment operator. You could rewrite your statement as: $message = $message . "Customer Phone Number is " . $phone; They're equivalent statements. The shorthand is used because, well, it's shorter. EDIT: So, what was the bad statement I corrected doing? Since you were using the normal assignment operator rather than the shorthand, you were simply overwriting your $message with that statement. However, your message still won't validate as you add your own strings to it before checking to see if a message exists at all, or if it's the right length. Do the check first, then add your text. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165012 Share on other sites More sharing options...
michaelramsgate Posted January 25, 2011 Author Share Posted January 25, 2011 Thanks for the reply and the explanation, I have a lot to learn. but... the form still does not validate the message. I take those 2 lines out and it validates it. I thought it might be because I am testing it offline in WAMP. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165016 Share on other sites More sharing options...
KevinM1 Posted January 25, 2011 Share Posted January 25, 2011 Show all of your current code. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165101 Share on other sites More sharing options...
michaelramsgate Posted January 25, 2011 Author Share Posted January 25, 2011 <?php include 'config.php'; error_reporting (E_ALL ^ E_NOTICE); $post = (!empty($_POST)) ? true : false; if($post) { include 'functions.php'; $name = stripslashes($_POST['contactname']); $email = trim($_POST['email']); $subject = "New Message"; $message = stripslashes($_POST['message']); $message .= "\n\n"; $message = $message . "Customer Phone Number is " . $phone; $error = ''; // Check name if(!$name) { $error .= 'Please enter your name.<br />'; } // Check email if(!$email) { $error .= 'Please enter an e-mail address.<br />'; } if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; } // Check message (length) if(!$message || strlen($message) < 15) { $error .= "Please enter your message. It should have at least 15 characters.<br />"; } if(!$error) { $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '<div class="notification_error">'.$error.'</div>'; } } ?> functions include <?php function ValidateEmail($email) { /* (Name) Letters, Numbers, Dots, Hyphens and Underscores (@ sign) (Domain) (with possible subdomain(s) ). Contains only letters, numbers, dots and hyphens (up to 255 characters) (. sign) (Extension) Letters only (up to 10 (can be increased in the future) characters) */ $regex = '/([a-z0-9_.-]+)'. # name '@'. # at '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains '.'. # period '([a-z]+){2,10}/i'; # domain extension if($email == '') { return false; } else { $eregi = preg_replace($regex, '', $email); } return empty($eregi) ? true : false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165106 Share on other sites More sharing options...
KevinM1 Posted January 25, 2011 Share Posted January 25, 2011 Replace your first code snippet with the following. Take note of what I do to $message, and how that matches what I said two posts ago. <?php error_reporting(E_ALL ^ E_NOTICE); include 'config.php'; $post = (!empty($_POST)) ? true : false; if($post) { include 'functions.php'; $name = stripslashes($_POST['contactname']); $email = trim($_POST['email']); $error = ''; // Check name if(!name) { $error .= 'Please enter your name.<br />'; } // Check email if(!$email) { $error .= 'Please enter an e-mail address.<br />'; } if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; } // Check message (length) if(!$message || strlen($message) < 15) { $error .= "Please enter your message. It should have at least 15 characters.<br />"; } if(!$error) { $subject = "New Message"; $message = stripslashes($_POST['message']); $message .= "\n\n"; $message .= "Customer Phone Number is " . $phone; $mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '<div class="notification_error">'.$error.'</div>'; } } ?> Also, where does $phone come from? Right now, you don't have any values being placed in that variable. Do you have a corresponding HTML form field for the phone number? If so, you need to grab a hold of that value in your script. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165115 Share on other sites More sharing options...
michaelramsgate Posted January 25, 2011 Author Share Posted January 25, 2011 phew, it works. I need to revisit, the differences in your code. Also, where does $phone come from? Right now, you don't have any values being placed in that variable. Do you have a corresponding HTML form field for the phone number? If so, you need to grab a hold of that value in your script. I do have the 'phone' value in the form, even though the phone variable is being inserted in the message, do I still need to define it, under the lists of variables in the top? Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165121 Share on other sites More sharing options...
KevinM1 Posted January 25, 2011 Share Posted January 25, 2011 Yup. You should have: $phone = $_POST['phone']; You should validate it as well. A regex should do the trick. EDIT: Just a quick word about form inputs and how they work with PHP - Input values are stored in one of two superglobal arrays - $_GET or $_POST (there's also a catch-all $_REQUEST, but that's sloppy and potentially dangerous to use). Which array is used depends on what method your HTML form will use. $_GET for get, $_POST for post. When you want to access a form value, you do it by accessing the superglobal array with the name of the form input you want to retrieve. So, $_POST['phone'] assumes that your form used the post method and had an input with the name of phone. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165131 Share on other sites More sharing options...
michaelramsgate Posted January 25, 2011 Author Share Posted January 25, 2011 thanks for all the help, really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/225556-elementary-php-question-sendmail-and-include-phone-field/#findComment-1165149 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.