I have a form where a receptionist enters a visitors information, first name, email etc and I want to send an email to the visitor using their email address entered in the form.
I have the php mail function working however it currently only sends to a specific sender that I manually specify. How do I make the submit button on this form send an email based on the contents of the email field?
I assume I need to do something like this
<?php
$to = $row['email'];
$subject = 'Welcome ' . $row['first_name'] . ';
$message = 'You have been booked in';
$headers = 'From: noreply#blah.com' . "\r\n" .
'Reply-To: noreply#blah.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I dont think the $row[] is correct as I want to pull from the form, not the table that the form is inputting into.
This is the form page:
//serve POST method, After successful insert, redirect to customers.php page.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//Mass Insert Data. Keep "name" attribute in html form same as column name in mysql table.
$data_to_store = filter_input_array(INPUT_POST);
//Insert timestamp
$db = getDbInstance();
$last_id = $db->insert ('tb_bookings', $data_to_store);
if($last_id)
{
$_SESSION['success'] = "Visitor signed in successfully!";
header('location: bookings.php');
exit();
}
}
//We are using same form for adding and editing. This is a create form so declare $edit = false.
$edit = false;
require_once('includes/admin-header.php');
?>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">Manual Sign-in</h2>
</div>
</div>
<form class="form" action="" method="post" id="visitor_form" enctype="multipart/form-data">
<?php include_once('../forms/prebook_form.php'); ?>
</form>
</div>
And this is the form:
<fieldset>
<div class="form-group">
<label for="f_name">First Name *</label>
<input type="text" name="first_name" value="<?php echo $edit ? $tb_bookings['first_name'] : ''; ?>" placeholder="First Name" class="form-control" required="required" id = "first_name" >
</div>
<div class="form-group">
<label for="l_name">Last name *</label>
<input type="text" name="last_name" value="<?php echo $edit ? $tb_bookings['last_name'] : ''; ?>" placeholder="Last Name" class="form-control" required="required" id="last_name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" value="<?php echo $edit ? $tb_bookings['email'] : ''; ?>" placeholder="E-Mail Address" class="form-control" id="email">
</div>
<div class="form-group">
<label>Visiting Date</label>
<input name="visiting_date" value="<?php echo $edit ? $tb_bookings['visiting_date'] : ''; ?>" placeholder="Visiting Date" class="form-control" type="date">
</div>
<div class="form-group">
<label>Visiting</label>
<input name="visiting" value="<?php echo $edit ? $tb_bookings['visiting_date'] : ''; ?>" placeholder="Who are they visiting?" class="form-control" id="visiting">
</div>
<div class="form-group text-center">
<label></label>
<button type="submit" class="btn btn-warning" >Save <span class="glyphicon glyphicon-send"></span></button>
</div>
</fieldset>
maybe something like this...
if (isset($_POST['submit'])) {
$to = $_POST['email'];
$name = $_POST['first_name'];
$subject = 'Welcome ' . $name;
$message = 'You have been booked in';
$headers = 'From: noreply#blah.com' . "\r\n" .
'Reply-To: noreply#blah.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Related
I have HTML pop up a contact form that I need help with to return a success message on the page or in the place of the send button.
<div class="form-popup" id="myForm">
<form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container">
<h1>Contact</h1>
<input type="text" placeholder="Name" name="name" required>
<input type="text" placeholder="Email" name="email" required>
<textarea rows="4" cols="33.5" placeholder="Your message..." name="message" required tabindex="35"></textarea>
<button type="submit" class="btn">Send</button>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</form>
</div>
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$from = 'From: mywebsite.com';
$to = 'sk#gmail.com';
$subject = 'Message from MyWebsite';
$headers = 'From: mywebsite.com' . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = "Name: $name\n Email: $email\n Message:\n $message";
?>
//please refer to my PHP code below that communicates with my HTML file.
currently after PHP submits my form it redirects to the homepage. I want
it to stay on same page and display Message sent Successfully in the send button.
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $headers)) {
header("Location: http://www.mywebsite.com");
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
This solution requies JQuery to run!
$('#myForm').submit(function(e){
//DO YOUR STUFF
e.preventDefault()//Remove this line if you want to submit the form
$('#send').hide();
$('.success').show();
});
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<div class="form-popup">
<form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container">
<h1>Contact</h1>
<input type="text" placeholder="Name" name="name" required>
<input type="text" placeholder="Email" name="email" required>
<textarea rows="4" cols="33.5" placeholder="Your message..." name="message" required tabindex="35"></textarea>
<div class="success" style="display:none;">
SUCCESS MESSAGE
</div>
<div class="buttons">
<button type="submit" class="btn" id="send">Send</button>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</div>
</div>
UPDATE for author:
<div class="form-popup">
<form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container">
<h1>Contact</h1>
<input type="text" placeholder="Name" name="name" required>
<input type="text" placeholder="Email" name="email" required>
<textarea rows="4" cols="33.5" placeholder="Your message..." name="message" required tabindex="35"></textarea>
<div class="success" style="display:none;">
SUCCESS MESSAGE
</div>
<div class="buttons">
<button type="submit" class="btn" id="send">Send</button>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</div>
</div>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$('#myForm').submit(function(e){
//DO YOUR STUFF
e.preventDefault()//Remove this line if you want to submit the form
$('#send').hide();
$('.success').show();
});
</script>
You should place php code above html and check if the request is POST or GET. the header("Location: http://www.mywebsite.com"); is redirecting to the home page. Instead of that create the variable that storing message(success or error) and if the request is POST show it on your form
<?php
$showMsg = false;
if ($_POST['submit']) {
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$from = 'From: mywebsite.com';
$to = 'sk#gmail.com';
$subject = 'Message from MyWebsite';
$headers = 'From: mywebsite.com' . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = "Name: $name\n Email: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $headers)) {
$showMsg = "SUCCESS";
} else {
$showMsg = "ERROR";
}
}
?>
<div class="form-popup" id="myForm">
<form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container">
<h1>Contact</h1>
<input type="text" placeholder="Name" name="name" required>
<input type="text" placeholder="Email" name="email" required>
<textarea rows="4" cols="33.5" placeholder="Your message..." name="message" required tabindex="35"></textarea>
<?php if( $showMsg) {?>
<?= $showMsg ?>
<?php } else { ?>
<button type="submit" name="submit" class="btn">Send</button>
<?php } ?>
<button type="button" class="btn cancel" onclick="closeForm()">Close</button>
</form>
</div>
When I try to click sent button, the content from the webpage is not redirecting to the .php page.I am using recaptcha in the form .Can you please help me to solve this issue..
my HTML code is:
<form action="sendform.php" id="contact-form" class="form-horizontal"
method="post">
<fieldset>
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Your Name</label>
<div class="col-sm-8">
<input type="text" placeholder="Your Name" class="form-control" name="name" id="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="email">Email Address</label>
<div class="col-sm-8">
<input type="text" placeholder="Enter Your Email Address" class="form-control" name="email" id="email">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="subject">Subject</label>
<div class="col-sm-8">
<input type="text" placeholder="Subject" class="form-control" name="subject" id="subject" list="exampleList">
<datalist id="exampleList" >
<option value="a">A</option>
<option value="b">B Combo</option>
</datalist>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="message">Your Message</label>
<div class="col-sm-8">
<textarea placeholder="Please Type Your Message" class="form-control" name="message" id="message" rows="3"></textarea>
</div>
</div>
<div class="col-sm-8" class="form-group" class="g-recaptcha" data-sitekey="xxxxxxyyyyyyy"></div>
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l">Submit</button>
<button type="reset" class="btn btn-primary">Cancel</button>
</div>
</fieldset>
</form>
And my PHP Code sendform.php
<?php
if (isset($_POST['submit']) && !empty($_POST['submit'])):
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'xxxxxxxxxxxxx';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if ($responseData->success):
$to = "aaa#abc.com"; // this is your Email address
$from = !empty($_POST['email']) ? $_POST['email'] : ''; // this is the sender's Email address
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$subject = !empty($_POST['subject']) ? $_POST['subject'] : '';
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From:" . $from;
$headers .= 'From:' . $name . ' <' . $from . '>' . "\r\n";
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
$succMsg = 'Your request have submitted successfully.';
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
I have tested your code and it works.
Please make sure that you have placed your html and php files in same directory and also your files should be served via a local running server.
So your url should look like this http://localhost/testing/index.html
Although, your sendform.php gives me captcha error ofcourse.
"Please click on the reCAPTCHA box."
I am having trouble with my contact form (from bootstrap). the php code as well as the html code are as seen below. Whenever I try the contact form, the body will be empty. Am I missing anything?
This is the html code:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
This is the PHP code:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'mail#luckystarmaids.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Your name and email form elements don't have name attributes. Without them, they won't be posted to the form's action1, 2.
Add names to your inputs:
<input type="text" name="first_name" class="input-block-level" required="required" placeholder="Your Last Name">
Also you will need to handle first and last name in your PHP code, at the moment you're only looking for name.
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input name=firstname type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input name=lastname type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input name=email type="text" class="input-block-level" required="required" placeholder="Your email address">
<label>Subject</label>
<input name=subject type="text" class="input-block-level" required="required" placeholder="Subject">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
Try also to handle the data on the server. Check at least if the values are send:
<?php
if ( isset( $_POST['firstname'] )
&& isset( $_POST['email'] )
&& isset( $_POST['subject'] )
&& isset( $_POST['message'] ) )
{
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'mail#luckystarmaids.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $s
ubject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
if ($success)
{
echo json_encode(array(
'success'=> true,
'message'=>'Email sent, you recieve an email at ' . $email
));
}
}
else
{
echo json_encode(array(
'success' => false,
'message' => 'An error has occured, please try again.'
));
}
?>
I am able to receive email with all the information except for the email address that is entered in the form, and when I receive the email, I get "No Sender" instead of the person's name or email.
This is my HTML:
<!-- Contact Form -->
<div class="col-xs-12 col-sm-8 col-md-8">
<div id="contant-form-bx" class="contant-form-bx">
<div class="contact-loader"></div>
<form action="mail.php" id="contact-form" class="contact-form" name="cform" method="post">
<div class="row">
<div class="col-md-6">
<label for="name" id="name_label">Name</label>
<span class="name-missing">Please enter your name!</span>
<input id="name" type="text" value="" name="name" size="30">
</div>
<div class="col-md-6 columns">
<label for="e-mail" id="email_label">Email</label>
<span class="email-missing">Please enter a valid e-mail!</span>
<input id="e-mail" type="text" value="" name="email" size="30">
</div>
<div class="col-md-12">
<label for="message" id="phone_label">Message</label>
<span class="message-missing">Say something!</span>
<textarea id="message" name="message" rows="7" cols="40"></textarea>
</div>
<div class="col-md-12 text-center">
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message">
</div>
</div>
</form>
</div>
</div>
This is my PHP:
<?php
// declare our variables
$name = $_POST['name'];
$email = $_POST['e-mail'];
$message = nl2br($_POST['message']);
// set a title for the message
$subject = "From $name via WEB";
$body = "From $name, \n\n$message";
$headers = 'From: '.$email.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// put your email address here
mail("info#wziel.com", $subject, $body, $headers);
?>
<!--Display a Thank you message in the callback -->
<div class="mail_response">
<h4>Thank you <?php echo $name ?>!</h4>
<p>I'll be in touch real soon!</p>
</div>
According to your form this:
$email = $_POST['e-mail'];
Should be this:
$email = $_POST['email'];
You have e-mail as the id not the name.
The php looks at name and not id
You're using $_POST['e-mail']. e-mail is the ID of the <input> tag. You should be using $_POST['email'] because name="email".
I have a PHP form that works except for 1 thing and I am going nuts trying to figure out how to make it work. It emails me the results, and everything displays in the email fine except the "Plus1" field is always blank. How can I get the value to display in the submitted email form? Thanks for your help.
You can see it live on http://michyandsmiley.com.
Here is the code for the form:
<div id="rsvp" class="text-center" data-scroll-reveal>
<div class="heading">
<h2>RSVP</h2>
<p><span></span><i class="fa fa-heart"></i><span></span></p>
</div>
<form role="form" name="contactform" action="process.php">
<div class="row">
<div id="name-group" class="form-group col-xs-12">
<label for="inputName">Your Name</label>
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="John Doe">
</div>
</div>
<div class="row">
<div id="email-group" class="form-group col-xs-12">
<label for="inputEmail">Your Email</label>
<input type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="name#domain.com">
</div>
</div>
<div class="row">
<div id="guests-group" class="form-group col-xs-6">
<label for="selectGuests">Total Guests</label>
<select class="form-control" name="selectGuests" id="selectGuests">
<option value="1" selected>1</option>
<option value="2">2</option>
</select>
</div>
<div id="plusone-group" class="form-group col-xs-6">
<label for="inputPlus1">Guest's Name</label>
<input type="Plus1" class="form-control" id="inputPlus1" name="inputPlus1" placeholder="Jane Doe">
</div>
<div class="row">
<div id="attending-group" class="form-group col-xs-12">
<label for="selectAttending">I am...</label>
<select class="form-control" name="selectAttending" id="selectAttending">
<option value="Attending" selected>So excited to attend! Yay!</option>
<option value="Not Attending">Sorry to miss it. Boo!</option>
</select>
</div>
</div>
<div class="row">
<button type="submit" class="btn btn-lg">Submit Your RSVP!</button>
</div>
</form>
</div>
And for the "process.php" code:
<?php
$send_to = '(removed my email for privacy purposes)';
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (empty($_POST['inputName']))
$errors['name'] = 'Name is required.';
if (empty($_POST['inputEmail']))
$errors['email'] = 'Email is required.';
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// if there are no errors process our form, then return a message
//If there is no errors, send the email
if( empty($errors) ) {
$subject = 'Wedding RSVP Form';
$headers = 'From: ' . $send_to . "\r\n" .
'Reply-To: ' . $send_to . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = 'Name: ' . $_POST['inputName'] . '
Email: ' . $_POST['inputEmail'] . '
Guests: ' . $_POST['selectGuests'] . '
GuestName: ' . $_POST['inputPlus1'] . '
Attending: ' . $_POST['selectAttending'];
$headers = 'From: RSVP Form' . '<' . $send_to . '>' . "\r\n" . 'Reply-To: ' . $_POST['inputEmail'];
mail($send_to, $subject, $message, $headers);
}
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Thank you!';
}
// return all our data to an AJAX call
echo json_encode($data);
You don't have the field in the field list to post to the server:
var formData = {
'inputName' : $('input[name=inputName]').val(),
'inputEmail' : $('input[name=inputEmail]').val(),
'selectGuests' : $('select[name=selectGuests]').val(),
'selectAttending' : $('select[name=selectAttending]').val()
};