Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently had only name,email,subject and message but really want to add phone number
help please

PHP
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['message']) && isset($_POST['phonenumber']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
 
  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to: )/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }
  
  //
  mail( "myemail@email.com", $_POST['subject'],  $_POST['message'],  "From:" . $_POST['email']);
}
?>
Posted
Updated 19-Mar-15 2:29am
v2
Comments
ZurdoDev 19-Mar-15 8:25am    
Where are you stuck? Just add the phone number into the subject or email or wherever you want.
kinahp 19-Mar-15 8:38am    
want to add on message but when i add like this its not even going email ,
mail( "myemail@email.com", $_POST['subject'], $_POST['message'], "From:" . $_POST['email'],$_POST['phonenumber']);
i have no idea where to add
ZurdoDev 19-Mar-15 8:42am    
Where is the mail function? Is that built into php or is it in your code?
ZurdoDev 19-Mar-15 8:42am    
Also, why do you want the phone number? What are you wanting to do with it?
kinahp 19-Mar-15 8:46am    
submit function

$("#contact-form").submit(function (e) {
e.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var message = $("#message").val();
var phonenumber = $("#phonenumber").val();
var dataString = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message + '&phonenumber=' + phonenumber;


function isValidEmail(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};

if (isValidEmail(email) && (message.length > 1) && (name.length > 1)) {
$.ajax({
type: "POST",
url: "../sendmail.php",
data: dataString,
success: function () {
$('.success').fadeIn(1000);
$('.error').fadeOut(500);
}
});
} else {
$('.error').fadeIn(1000);
$('.success').fadeOut(500);
}

return false;
});

want to add phone number on email message when user fills up on contact form

1 solution

HI
please make the below mentioned changes in your code.
$MESSAGE_BODY .= "Contact No: ".$_POST["phonenumber"]."\n"; 
$MESSAGE_BODY .= "Message: ".$_POST["message"])."\n";

mail( "youremail@email.com", $_POST['subject'], $MESSAGE_BODY, "From:" . $_POST['email']);

reference:"http://php.net/manual/en/function.mail.php"
 
Share this answer
 
Comments
kinahp 20-Mar-15 5:29am    
thank you ramyajaya, but it doesn't work either
ramyajaya 20-Mar-15 5:46am    
does it show any error in your error console? Or you are receiving mails without phone number?
kinahp 20-Mar-15 6:54am    
doesn't show any error, but not even receiving any email if I add this
ramyajaya 20-Mar-15 9:16am    
Could you post ur working code and ur change there might be only a minor issue
kinahp 20-Mar-15 9:48am    
$val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}

mail( "email@email.com", $_POST['subject'], $_POST['message'], "From:" . $_POST['email']);


}
?>

Currently this is working fine apart phone number

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900