wordpress ajax form submition - php

I need to send the mail in wordpress for the custom ajax form. The mail
should contain full name, email, phone number and the
message(comment). Please help me to do it.
I have included the html code as a shortcode in functions.php .
function send_grivence(){
var form1 = jQuery("#myform").serialize();
console.log(form1);
jQuery.ajax({
data: {action: 'send_form', form:form1},
type: 'post',
url: 'mydomain.com/wp-admin/admin-ajax.php/',
success: function(data) {
console.log(data); //should print out the name since you sent it along
}
});
}
<form class="form-horizontal" id="myform">
<fieldset>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="fullname">Fullname:</label>
<div class="controls">
<input type="text" id="fullname" name="fullname" placeholder="your-name" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="email">Email Id:</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="your-email" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="number">Phone No:</label>
<div class="controls">
<input type="text" id="phonenumber" name="phonenumber" placeholder="your-phone no" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="number">Comment:</label>
<div class="controls">
<textarea name="name" rows="8" cols="80" placeholder="your-message"></textarea>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<a onclick="send_form()" class="btn btn-success">Submit</a>
</div>
</div>
</fieldset>
</form>

first jquery code to submit form via ajax
jQuery(document).ready(function (jQuery) {
jQuery('#myform').submit(contactSubmit);
function contactSubmit() {
var contactForm = jQuery('#myform').serialize();
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: contactForm,
success: function (data) {
// console.log(id);
console.log(data);
}
});
return false;
}
});
make sure that you place a action input in your form like this
<input type="hidden" name="action" value="contactForm"/>, just paste it before submit button. Note that contactForm function is called in function.php
then place a function in your function.php
function contactForm() {
$name = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$message = $_POST['comment'];
$quote = 'Full Name: ' . $name . "\n\r" . 'Email: ' . $email . "\n\r" . 'Phone: ' . $phone . "\n\r" . 'Message: ' . $message . "\n\r";
$to = "YOUR EMAIL ADDRESS"; // put your email here
$headers = 'From:' . $name . '<' . $email . '>' . "\r\n"; // put user's email here or duplicate your email
$subject = 'Contact Form';
if ( wp_mail( $to, $subject, $quote, $headers ) === false ) {
echo "Error";
} else {
echo "<h3> Mail Sent Successfully Done</h3>";
}
die();
}
add_action( 'wp_ajax_contactForm', 'contactForm' );
add_action( 'wp_ajax_nopriv_contactForm', 'contactForm' );
Also make sure you replace your email in "YOUR EMAIL ADDRESS". also, make sure your url: "/wp-admin/admin-ajax.php", displays your full address. In local you might include url: "/YOUR_FOLDER_HERE/wp-admin/admin-ajax.php", else in live server it works fine.
EDIT
<textarea name="name" rows="8" cols="80" placeholder="your-message"></textarea>
change name="name" to name="comment"
Hope this helps.

Related

Jquery- Putting an attachment in form and send to email

I'm having a problem on how to add an attachment in my working form, i'm using Bootstrap, JQuery and PHP. i have no idea on how to add an attachment where the user will attach their Resume's and then send it to email.
i hope you can help me with this without changing my entire code. is it even possible?.
This is my initial input form
<section class = "resume section-padding" id = "resume "><div class="form-group col-md-6">
<label for="fName">First Name</label>
<input type="text" class="form-control" id="fName" placeholder="First Name">
</div>
<div class="form-group col-md-6">
<label for="lName">Last Name</label>
<input type="text" class="form-control" id="lName" placeholder="Last Name">
</div>
<div class = "form-group col-md-12">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" placeholder="Address">
</div>
<div class = "form-group col-md-4">
<label for="doBirth">Date of Birth</label>
<input type="date" class="form-control" id="doBirth" placeholder="Date of Birth">
</div>
<div class = "form-group col-md-4">
<label for="city">City</label>
<input type="text" class="form-control" id="city" placeholder="City">
</div>
<div class = "form-group col-md-4">
<label for="zipCode">ZIP CODE</label>
<input type="number" class="form-control" id="zipCode" placeholder="ZIP">
</div>
<div class = "form-group col-md-6">
<label for="email">E-mail</label>
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class = "form-group col-md-6">
<label for="phone">Phone</label>
<input type="number" class="form-control" id="phone" placeholder="Phone Number">
</div>
</div>
This is my jquery it will get the value of the form.
$('.resume #getResume').on('click', function(e){
e.preventDefault();
var fName = $('#fName').val();
var lName = $('#lName').val();
var address = $('#address').val();
var doBirth = $('#doBirth').val();
var city = $('#city').val();
var zipCode = $('#zipCode').val();
var email = $('#email').val();
var phone = $('#phone').val();
var form = new Array({ 'fName':fName, 'lName':lName, 'address':address, 'doBirth':doBirth, 'city':city, 'zipCode':zipCode,
'email':email, 'phone':phone });
$.ajax({
type: 'POST',
url: "/path/to/url.php",
data: ({'action': 'resume', 'form': form})
}).done(function(data) {
$('#resume .result').html(data);
$(".resume")[0].reset();
});
});
This is my php this where my jQuery will pass the value
$sendTo = "email#email.com";
$action = $_POST['action'];
$name = $_POST['form'][0]['name'];
$email = $_POST['form'][0]['email'];
$subject = $_POST['form'][0]['subject'];
$message = $_POST['form'][0]['message'];
if ($name == "" || $email == "" || $message == "" || $subject == "") {
echo "<p class=\"error\">There was problem while sending E-Mail. Please verify entered data and try again!</p>";
exit();
}
else
{
$header = 'From: ' . $name . '<' . $email . ">\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sent = mail($sendTo, $subject, $message, $header);
if ($sent) {
echo "<p class=\"success\">Message sent succesfully.</p>";
} else {
echo "<p class=\"error\">There was problem while sending E-Mail.</p>";
}
}
?>
If you are not using any framework and in core php it is very easy to upload a file. You can see all the code and working here for better understanding. This is good source to get thing done.
In your current code just add a field to upload a file. Like this
<input type="file" name="fileToUpload" id="fileToUpload">
. Also dont forget to ON the file_uploads in php.ini file.
To add it in the email:
for adding in the email just catch the file in the script file and send it to the email like this.
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

PHP not sending email from my website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I've the following contact form in my website. But the form data is being appended onto the URL and the json is not being sent to the PHP mail handler. Below are my code for the form.
HTML
<form novalidate="novalidate" style="width:60%;margin-left:auto;margin-right:auto;" onsubmit="sendMailNew()">
<div class="col-md-12">
<div class="col-md-6" style="padding-right: 5px">
<input type="text" class="form-control" name="contact_uname" id="contact_uname" placeholder="NAME" style="width:100%">
</div>
<div class="col-md-6">
<input type="text" class="form-control" name="contact_no" id="contact_no" placeholder="PHONE NUMBER" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input class="form-control" type="email" name="contact_email" id="contact_email" placeholder="E-MAIL ADDRESS" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_company" id="contact_company" placeholder="COMPANY" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_website" id="contact_website" placeholder="EXISTING WEBSITE(if any)" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_social" id="contact_social" placeholder="WHAT WOULD YOU LIKE(website, social media etc)" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_budget" id="contact_budget" placeholder="BUDGET(if we know approximately how much you're hoping to spend. we can (hopefully) come up with a strategy that fits your budget.)" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_section" id="contact_section" placeholder="WHAT SECTION WOULD YOU LIKE ON THE SITE" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_update" id="contact_update" placeholder="WHICH OF THOSE SECTIONS WILL YOU WANT TO BE ABLE TO UPDATE YOURSELF?" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_design" id="contact_design" placeholder="ANY IDEA ON THE STYLE OT DESIGN? DO YOU HAVE REFERENCES?" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_timeline" id="contact_timeline" placeholder="TIMELINE(when do you want to launch?)" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_hear" id="contact_hear" placeholder="HOW DID YOU HEAR ABOUT US?" style="width:100%">
</div>
<div class="col-md-12" style="margin-top: 3%">
<input type="text" class="form-control" name="contact_comment" id="contact_comment" placeholder="ANY OTHER COMMENTS?" style="width:100%">
</div>
<div class="col-md-12" style="margin-top:3%">
<button type="submit" name="submit" value="submit" style="position:relative;left:45%;color:#22252c !important;background-color:white !important" class="btn btn-default">SUBMIT NOW</button>
</div>
</div>
</form>
AJAX script
<script type="text/javascript">
function sendMailNew()
{
$.ajax({
type: 'POST',
url: 'contactmail.php',
data: { cname: $("#contact_uname").val(), cno: $().val("#contact_no"), cemail: $("#contact_email").val(), ccompany: $("#contact_company").val(), cwebsite: $("#contact_website").val(), csocial: $("#contact_social").val(), cbudget: $("#contact_budget").val(), csection: $("#contact_section").val(), cupdate: $("#contact_update").val(), cdesign: $("#contact_design").val(), ctimeline: $("#contact_timeline").val(), chear: $("#contact_hear").val(), ccomment: $("#contact_comment").val() },
async: false,
success: function (data) {
$("#contact_uname").val()=data;
$("#contact_no").val()="";
$("#contact_email").val()="";
$("#contact_company").val()="";
$("#contact_website").val()="";
$("#contact_social").val()="";
$("#contact_budget").val()="";
$("#contact_section").val()="";
$("#contact_update").val()="";
$("#contact_design").val()="";
$("#contact_timeline").val()="";
$("#contact_hear").val()="";
$("#contact_comment").val()="";
},
dataType: 'json',
error: function (e, g, y) {
var msg = e;
}
});
alert('Thanks for submitting the form, we will get back to you soon!');
}
</script>
PHP mail handler
<?php
if(isset($_POST['submit'])){
$to = "pavan#tip.agency";
$from = $_POST['contact_email']; // this is the sender's Email address
$name = $_POST['contact_uname'];
$number = $_POST['contact_no'];
$company = $_POST['contact_company'];
$website = $_POST['contact_website'];
$social = $_POST['contact_social'];
$budget = $_POST['contact_budget'];
$section = $_POST['contact_section'];
$update = $_POST['contact_update'];
$design = $_POST['contact_design'];
$timeline = $_POST['contact_timeline'];
$hear = $_POST['contact_hear'];
$comment = $_POST['contact_comment'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = "Name:" . $name . "\n Email: " . $from . "\n Number:" . $number . "\n Company:" . $company . "\n Website:" . $website . "\n Social:" . $social . "\n Budget:" . $budget . "\n Section:" . $section . "\n Update:" . $update . "\n Design:" . $design . "\n Timeline:" . $timeline . "\n Hear:" . $hear . "\n Comment:" . $comment;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
}
?>
Any idea how to fix this?
Thanks in advance!!
Stop default action in javascript first.
<script type="text/javascript">
function sendMailNew()
{
event.preventDefault();
//Your rest of JS here
}
</script>
I'm not sure but you may need to add action="" to the form. Let us know if it solves.
the option is not type: 'POST',
it is method: 'POST'. So by default it should be a GET request.
In your PHP Script, you take the values from "$_POST", so it can not find your parameters. Just try it this way. I prefer to use $.post, not $.ajax for my post requests. To make the script better, you should prevent default event and maybe use the url to submit your data as attribute action, and take it by $('form').attr('action') for your request.
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
I prefer this way, it looks just cleaner and you can not make a misstake with submit method:
function sendMailNew(e)
{
e.preventDefault();
var $form = $('form');
var url = $form.attr('action') || 'contactmail.php';
var params = {
cname: $("#contact_uname").val(),
cno: $().val("#contact_no"),
cemail: $("#contact_email").val(),
ccompany: $("#contact_company").val(),
cwebsite: $("#contact_website").val(),
csocial: $("#contact_social").val(),
cbudget: $("#contact_budget").val(),
csection: $("#contact_section").val(),
cupdate: $("#contact_update").val(),
cdesign: $("#contact_design").val(),
ctimeline: $("#contact_timeline").val(),
chear: $("#contact_hear").val(),
ccomment: $("#contact_comment").val()
};
$.post(url, params, function(data, status) {
$("#contact_uname").val()=data;
$("#contact_no").val()="";
$("#contact_email").val()="";
$("#contact_company").val()="";
$("#contact_website").val()="";
$("#contact_social").val()="";
$("#contact_budget").val()="";
$("#contact_section").val()="";
$("#contact_update").val()="";
$("#contact_design").val()="";
$("#contact_timeline").val()="";
$("#contact_hear").val()="";
$("#contact_comment").val()="";
}, 'json').fail(function() {
alert( "error" );
});
}

unsuccessful clearing of form values/content after successful submission

Each time, I submit my form, the expected reset function is not clearing my form. Please how should I clear my form my codes(HTML, PHP, JS) follows below.
HTML codes:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
Javascript code is below:
jQuery(function($) {'use strict',
......
......
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: formData,
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
$("#main-contact-form")[0].reset();
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
......
......
});
And last of all is my PHP code:
<?php
$status = array(
'type'=>'success',
'message'=>'Thank you for contacting us. As soon as possible we will contact you!'
);
$fail = array(
'type'=>'fail',
'message'=>'Please try again. Your mail could not be delivered!'
);
$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 = 'email#mail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
header('Content-type: application/json');
if($success){
echo json_encode($status);
}
else{
echo json_encode($fail);
}
?>
Try this code
$("#main-contact-form")[0].reset();
One important thing i was actually missing was that I actually used the reset function before I outputted the obtained message from the server.Looking at my code above, one will discover that i have in my javascipt code the line below:
.done(function(data){
$("#main-contact-form")[0].reset();
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
});
});
which I corrected to become this one below:
.done(function(data){
form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
$('#main-contact-form')[0].reset();
//$('#main-contact-form').trigger("reset");
});
A BIG THANKS TO YOU GUYS!

Send mail from HTML form with PHP backend

I am working on my personal website at the moment, and I have done everything, except when a message comes through from a user via the contact form, and email is successfully sent to me, but the content contained within said email is blank.
My code is below:
PHP (sendmail.php)
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Your email has been succesfully 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 = 'me#joshuaquinlan.co.uk';
$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;
HTML (contact-us.html):
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendmail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" required="required" placeholder="Email address">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
When the email is actually sent, however, it actually shows up like
this
Please, if someone can help me, then thank you!
Your issue is you are not sending anything from your form:
$.post($(this).attr('action'),function(data){$this.prev().text(data.message) // ...
You forgot to add the data:
$.post($(this).attr('action'),$(this).serialize(), function(data){$this.prev().text(data.message) // ...
Beautified Code
// You forgot to add data here v----
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
So correct it to:
// added data using serialize() vvvvvvvvvvvvvvvvvv
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
Find and Replace
Press Ctrl + H (Replace).
Paste this in Find For: $.post($(this).attr('action'),func.
Paste this in Replace With: $.post($(this).attr('action'),$(this).serialize(),func.
Press Replace.
See below screenshot for more details.
Screenshot

Contact form won't submit data

I have a JSON code in a sendemail.php file for my Bootstrap 3 contact form. The form is able to send an email to the specified email address but there is no data being submitted. I get blank emails with labels and the header 'unknown sender'. Please help solve this.
This is the bootsrap form code chunk
<div class="col-sm-6">
<h1>Contact Form</h1>
<p>Please contact us using the form or our contact details are available here if you'd like to contact us via Email or Phone.</p>
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="Name" class="form-control" required placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="Email" class="form-control" required placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="Message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
</div>
</div>
</div>
</form>
</div><!--/.col-sm-6-->
This is the main.js code where the AJAX for contact form is present
jQuery(function($) {
$(function(){
$('#main-slider.carousel').carousel({
interval: 5000,
pause: false
});
});
//Ajax contact
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
//smooth scroll
$('.navbar-nav > li').click(function(event) {
if(!$( this ).attr( 'href' ).match(/^#/)) return;
event.preventDefault();
var target = $(this).find('>a').prop('hash');
$('html, body').animate({
scrollTop: $(target).offset().top
}, 500);
});
//scrollspy
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})
//PrettyPhoto
$("a.preview").prettyPhoto({
social_tools: false
});
//Isotope
$(window).load(function(){
$portfolio = $('.portfolio-items');
$portfolio.isotope({
itemSelector : 'li',
layoutMode : 'fitRows'
});
$portfolio_selectors = $('.portfolio-filter >li>a');
$portfolio_selectors.on('click', function(){
$portfolio_selectors.removeClass('active');
$(this).addClass('active');
var selector = $(this).attr('data-filter');
$portfolio.isotope({ filter: selector });
return false;
});
});
});
This is the sendemail.php code
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = $_POST['Name'];
$email = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$email_from = $email;
$email_to = 'myemail#domain.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;
?>
You are missing the name attribute in your html input tags
This is required for the server side variables, for example:
$name = $_POST['Name'];
will get value from
<input type="text" name="Name" class="form-control" required placeholder="Name">
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input name="Name" type="text" class="form-control" required placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input name="Email" type="text" class="form-control" required placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="Message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg" value="Send Message">
</div>
</div>
</div>
</form>
<?php
if(isset($_POST['submit'],$_POST['Name'],$_POST['Email'],$_POST['Subject'],$_POST['Message'])){
$name = $_POST['Name'];
$email = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$email_from = $email;
$email_to = 'myemail#domain.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.'>');
$response = array();
if($success){
$response['success'] = true;
$response['message'] = 'Email sent!';
}else{
$response['success'] = false;
$response['message'] = 'Email sent!';
}
}else{
$response['success'] = false;
$response['message'] = 'post variables are not set';
}
echo json_encode($response);
//die;
Change this line in main.js:
$.post($(this).attr('action'), function(data) {
to:
$.post($(this).attr('action'), $(this).serialize(), function(data) {
It will now submit the data.

Categories