I'm using code from another source but it's very different to what I'm used to coding when using standard HTML/PHP websites. Wordpress seems to have a mind of it's own. Basically I need the contact form's PHP to send the name and email within the body text of the email and not just the persons message. Currently all that comes through is the persons message.
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please Fill in all Required Fields.";
$email_invalid = "Your Email Address is Invalid.";
$message_unsent = "Message was not sent. Please Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "New Enquiry Through Mon Voyage Website";
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
Any ideas?
add this in just before if(!$human == 0){:
$message.='Name: '.$name.'email: '$email;
this is all standard php form checking, 1. function to display a reponse 2. check the $_POST data 3. fail if errors in place 4. if no errors, send email. Put a few echo's in there and play around with it to see what is happening.
This does not solve the issue you are having directly but instead of writing the code yourself you could use Contact form 7 plugin instead. It will save you time and is very quick to implement.
You can get the plugin from this url.
Here is the documentation for implementing the plugin.
You can add a contact form by adding the short code generated by the plugin to the page you want it to appear like this:
[contact-form-7 id="1234" title="Contact form 1"]
Related
I want to send the email from the server when someone submits the contact form. Here is the sample php code.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$name = strip_tags(trim($_POST['name']));
$email = strip_tags(trim($_POST['email']));
$dept = strip_tags(trim($_POST['dept']));
$message = strip_tags(trim($_POST['message']));
if((!isset($email) || empty($email)) || (!isset($name) || empty($name)) || (!isset($dept) || empty($dept)) || (!isset($message) || empty($message))){
echo -1;
return;
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){
echo -2;
return;
}
$to = 'farooqahmadkhan003#gmail.com';
$email_subject = "Splendenti Eye Wear | Contact Us";
$email_body = "Contacted by\nName: $name\nEmail: $email\nDepartment: $dept\nMessage: $message";
$headers = "noreply#farooq.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: ".$_POST['email'];
if(mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
else{
echo "failed to send email";
}
}else{
echo "Invalid Request";
}
?>
but the mail function is returning false. Can anybody guide??
I suggest you to use PHPMailer to send emails from your PHP files.
Check this link and download it. There are many documentations about PHPMailer online. You can control all the details about your mail with PHPMailer.
try to use # to suppress warnings:
if(#mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
If this doesn't help you have a configuration issue.
I am building a set of forms shortcodes in Wordpress. I have given up trying to get the form processing script into a shortcode and am just going to make it available to all pages.
There is however an echo that puts out the response of the form which I want to put into it's own shortcode.
<?php echo $response; ?>
This needs to go at the top of the form so that validation messages appear in the right place.
Total form processing code:
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='callout success'><p>{$message}</p></div>";
else $response = "<div class='callout error'><p>{$message}</p></div>";
}
// response messages
$not_human = "Please fill out the human verification field to prove you are not a spam robot. A number 2 will do the job nicely.";
$missing_content = "It looks like we are missing something. Please chek the form below.";
$email_invalid = "That email Address doesn't look quite right.";
$message_unsent = "Your message wasn't sent. Please have another go. If it still doesn't work then please call us using the number supplied.";
$message_sent = "Thank you for your enquiry. We will be in contact shortly.";
// user posted variables
$business_name = $_POST['message_business_name'];
$first_name = $_POST['message_first_name'];
$last_name = $_POST['message_last_name'];
$email = $_POST['message_email'];
$phone = $_POST['message_phone'];
$human = $_POST['message_human'];
$location = $_POST['location'];
$message_opt_in = $_POST['message_opt_in'];
$optin = "";
if ($message_opt_in == "on"){
$optin = "The user has opted in to marketing emails";
}
else {
$optin = "!!!!!!!!!!!!!!!!!!!!!!\n\n The user has NOT opted in to marketing emails! \n\n!!!!!!!!!!!!!!!!!!!!!!\n\n";
}
$body = "$optin \n\n\n\n This message was sent from xxx.com $location\n\nBusiness name: $business_name \nName: $first_name \nName: $last_name \nEmail: $email \nPhone: $phone";
//php mailer variables
$to ="xxx#xxx.com";
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($business_name) || empty($first_name) || empty($email)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($body), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
<?php echo $response; ?>
I have tried amongst other things that would probably make a PHP dev laugh.
function form_response(){
echo $response;
}
function form_response(){
echo '<?php echo "$response"; ?>';
}
Disclaimer I'm not a PHP developer as you can probably tell.
In the my_contact_form_generate_response function, you can do return $response; at the very bottom before the closing } for the function.
Then, you can do echo my_contact_form_generate_response($type, $message); and it will echo the return value, which will be $response in this case.
Try to change
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
To
else if ($_POST['submitted']){
$response = 'Some things';
$response .= my_contact_form_generate_response("error", $missing_content);
return $response;
}
I had to call $response from global
function form_response(){
echo $GLOBALS["response"];
}
I've found a template that I want to edit and it already has a nice looking contact form. After hitting the submit button I'm getting a thank you message but I would like to redirect to another page where the message will appear so I can use it for conversion tracking.
Could someone be of assistance what should I do as my current code looks like this:
<?php
$firstname = '';
$number = '';
$message = '';
$email = '';
if($_POST) {
// collect all input and trim to remove leading and trailing whitespaces
$firstname = trim($_POST['first_name']);
$number = trim($_POST['phone']);
$message = trim($_POST['message']);
$email = trim($_POST['email']);
$errors = array();
// Validate the input
if (strlen($firstname) == 0)
array_push($errors, "Please enter your name");
if (strlen($number) == 0)
array_push($errors, "Please specify your number");
if (strlen($message) == 0)
array_push($errors, "Please enter the details of your message");
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
array_push($errors, "Please specify a valid email address");
// If no errors were found, proceed with storing the user input
if (count($errors) == 0) {
//completion message into array
$to = 'me#mymailzz.com'; // note the comma
// the email subject ( modify it as you wish )
$subject = "Enquiry From website";
// the mail message ( add any additional information if you want )
$msg = "Quote From website:\n-------------------------------------------\n Name: $firstname \n Number: $number \n Email: $email \n Message: $message \n-------------------------------------------";
//function to send email
try {
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
$return1 = "Thank You for sending your message, we will be in touch within 48-72 hours";
array_push($errors, $return1);
$formsubmit = 1;
}catch(Exception $e){
$return3 = "<center><p><b>Your message did not get sent due to an error. Please try again. Caught Exception {$e->getMessage()}</center>";
array_push($errors, $return3);
$formsubmit = 0;
}
}
//Prepare errors for output
$output = '';
foreach($errors as $val) {
$output .= "<p class='output'>$val</p>";
}
}
?>
Replace the line:
$return1 = "Thank You for sending your message, we will be in touch within 48-72 hours";
With something like this:
header('Location: some_page.php');
exit;
And then you can remove these lines as they become obsolete:
array_push($errors, $return1);
$formsubmit = 1;
That will redirect to some_page.php instead of printing that message.
To store the message across pages, do this at the start of your script:
session_start();
Instead of storing messages in a normal variable, store it in a session variable, e.g.:
$_SESSION['message'] = 'Your message here';
At the end of your script:
if ($formsubmit == 1) {
header('Location: your-file.php');
}
In your-file.php:
<?php
session_start();
echo $_SESSION['message'];
Just made a change in your try code and insert redirect fuunction
try {
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='next_page.php'
</SCRIPT>");
}
How about you trace in your web template where you have <form action="#"> and proceed with help from a previously similar asked question found here
I am a complete novice when it comes to php, I have got the below php which sends me back the 'message' and sends an auto response to the user, as well as redirecting them to the 'thank you' page. Problem I am having is that it won't return the users name that they fill in on the form, any ideas?
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.', $message, "From: Krew Kut Hair<$email>")) {
$autoreply = "Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Assuming the name is in one of the form fields, you should be able to retrieve it. As Barmar says - all you have to do is use it somewhere in the body or the message. How can you tell the name is missing if you don't echo it out somewhere.
Try this:
$autoreply = "Thank you ".$name." for ...
If the name is still "missing" - you can try to see all the post variables like this:
echo "<PRE>Post Vars\n"; print_r($_POST);
If you have an input named "name" like:
<input type="text" name="name" value="" />
Check if it's containing data with e.g. :
echo 'The value of name is ['.$name.']';
If it is containing data you just can use the $name variable in your message. If it isn't there is probably something wrong in your HTML form.
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$content = "<strong>Name:<strong><br />".$name."<br />";
$content .= "<strong>Message:<strong><br />".$message."<br />";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.<br />', $content, "From: Krew Kut Hair<$email>")) {
$autoreply = "Hi ".$name.". Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Also read the comments on your question. I strongly recommend to find an other way instead of using extract().
I have decided I'd like to incorporate localStorage on my WordPress contact page form. I am using the jQuery and PHP script below to store unsent text within the form and then once completed send the information. The script works for the most part it does send and give error messages when not fully completed. The problem I'm encountering is when the information in the form fields are correct and sent the fields are not clearing after the browser refresh.
I've tried modifying this line of script which is suppose to clear the fields but I've had no luck .
//My field id's
var formElements = $('#name, #message, #email');
//on form submit remove item from localstorage
formElements.submit(function(e){
e.preventDefault();
formElements.each(function(){
localStorage.removeItem($(this).attr('id'));
});
});
PHP Script:
<?
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
jQuery Script :
jQuery(function($) {
var formElements = $('#name, #message, #email');
formElements.on('keyup', function(){
//put value in the localStorage
localStorage.setItem($(this).attr('id'), $(this).val());
});
//on form submit remove item from localstorage
formElements.submit(function(e){
e.preventDefault();
formElements.each(function(){
localStorage.removeItem($(this).attr('id'));
});
});
//if form not submitted and page refreshed get and set the values
formElements.each(function(){
$(this).val(localStorage.getItem($(this).attr('id')));
});
});
Your call to set formElements is not correct. It should be:
var formElements = $('#name, #message, #email');
The list of selectors should all be in one string, not separate arguments to jQuery.