Send mail from HTML form with PHP backend - php

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

Related

Page is not redirecting to the php page from Html, when send button click in the web form?

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 unable to retrieve data from a $_POST or $_GET on an HTML form with PHP

I've been all over stack overflow and other various google searches. I have not been able to figure out why this is happening.
I receive emails and any plaintext that is hard coded in the PHP file works fine. However, I am unable to use $_GET or $_POST to pull the data from the HTML form. Any suggestions would be greatly appreciated.
Form submission code:
<form action="sendemail.php" method="post" id="main-contact-form" class="contact-form" name="contact-form" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="username" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="useremail" class="form-control" required="required" placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea type="text" name="usermessage" id="message" required="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"></input>
</div>
</div>
</div>
</form>
PHP page code:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = $_POST['username'];
$email = $_POST['useremail'];
$subject = 'Webform Submission.';
$message = $_POST['usermessage'];
$email_from = $email;
$email_to = 'test#test.com';
$body = "Name: " . $name . "\n\n" . "Email: " . $email . "\n\n" . "Subject: " . $subject . "\n\n" . "Message: " . $message;
$success = #mail($email_to, $subject, $body);
echo json_encode($status);
die;
?>
This is the Data that gets sent in the e-mail:
Name:
Email:
Subject: Webform Submission.
Message:
Update: I've been messing around and I am still not able to get any data grabbed from the index.php file. This is what I used to test this theory:
<?php
$raw_post = file_get_contents('php://input');
var_dump($raw_post);
?>
As well as:
<?php
var_dump($_POST);
?>
I am starting to think it is a server setting, involving the php.ini or the .htaccess file. However, none of the suggestions I have found have worked. I am still at a loss for this issue.

Not receiving POST in PHP when using class contact-form from Bootstrap3

I have a simple contact form in Bootstrap3. When I add the class "contact-form" the php is not receiving POST parameters, so the mail is sent void.
This is the form:
<div id="contact-section">
<h3>Contacta con nosotros</h3>
<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">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Nombre">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="E-mail">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="4" placeholder="Introduzca su mensaje"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right">Enviar</button>
</div>
</form>
</div>
and this is the php:
<?php
header('Content-type: application/json');
$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 = 'jcarlosrga#gmail.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.'>');
$status = array(
'type'=>'success',
'message'=>'Gracias por el mensaje. Tan pronto como podamos, nos pondremos en contacto contigo.'.$name
);
echo json_encode($status);
die;
?>
This is the jquery validation
// Contact form validation
var form = $('.contact-form');
form.submit(function () {'use strict',
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
As far as I know, contact-form is not a valid bootstrap class, try using form-horizontal.
If you defined the contact-form class somewhere, please share it with us.
Adding a class to the form cannot really preventing it from sending post data, unless you have a typo in your code and you're missing ending quotes or something like that (but it seems ok in your question).
To test this hypothesis, move the method and action attributes at the beginning, like this:
<form method="post" action="sendemail.php" id="main-contact-form" class="contact-form" name="contact-form" >

FILTER_VALIDATE_EMAIL saying a valid email is invalid

I have a problem with checking if a email is valid. but the weird is that i have the same form on to different pages/urls, and on one of the forms it keeps saying that the email is invalid and on the form its valid.
The form on this page works - http://night.sendme.to/about
the form on this page doesnt - http://night.sendme.to/book/jokeren
The HTML on the forms is the same
<form action="" method="post" id="myform">
<div class="form-group">
<label for="name">Navn *</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Navn" required="required">
</div>
<div class="form-group">
<label for="corp">Virksomhed</label>
<input type="text" class="form-control" id="corp" name="corp" placeholder="Virksomhed">
</div>
<div class="form-group">
<label for="email">Email adresse *</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email adresse" required="required">
</div>
<div class="form-group">
<label for="tel">Telefon *</label>
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefon" required="required">
</div>
<div class="form-group">
<label for="message">Kommentar</label>
<textarea class="form-control" id="message" name="message" rows="10" required="required"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit">Send</button>
</form>
<div id="success" style="color:red;"></div>
The PHP is this
<?php // Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOURMAIL';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:".$_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $header); //This method sends the mail.
echo "Your email was sent!";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
} else {
echo "Invalid Email, please provide an correct email.";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
The javascript is this
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", $("#myform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
Hope some one can help, why the form only works on the http://night.sendme.to/about and the others
So, to not leave this question answer-less:
In your HTML code, you actually had <form action="" method="post" id="myform"> in both pages – but in your second page, you had another <form> tag right before it … and because of this invalid HTML, the browser ignored the second form tag, and that made $("#myform").serialize() not return any data at all, because it could not find the form element with that id.
You should always validate your HTML code. This helps avoiding such errors.

Post Data is not coming

Update Two: After doing some testing, I found that it does not work when jQuery is loaded in the page. My page relies on jQuery, so what should I do?
Update: You can see the problematic page here here, and the working test page here.
I'm making a contact form, and it looks like this
<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 name="first" type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input name="last" 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">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" 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>
Nothing too fancy. sendemail.php looks like this
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['first']." ".$_POST['last']));
$email = #trim(stripslashes($_POST['email']));
$subject = "Monarc - Message From ".$name;
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'xxx#xxx.xxx';
$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;
The email comes through, but the postdata isn't coming through in the email. It says (unknown sender) as the sender, and the body looks like this:
Name:
Email:
Subject: Monarc - Message From
Message:
What am I doing wrong?
Thanks!
To post the form value you must use input type submit instead of button type
change:
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
to:
<input type="submit" class="btn btn-primary btn-large pull-right" value="Send Message">
supplementary information, this form uses the following javascript file:
jQuery(function($) {
//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; });
//Goto Top $('.gototop').click(function(event) {
event.preventDefault(); $('html, body').animate({
scrollTop: $("body").offset().top }, 500); }); //End goto top
});
Searching on another forum I found how to fix this. You only need add the following code in the php file:
$headers = "From: mail <$email_from>\r\n";
$headers .= "MIME-Version: 1.0" ."\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
and remove "#" of $success = #mail($email_to, $subject, $body,'From: <'.$email_from.'>');
In "main.js" file your code isn't supplying any data in the .post() method. you need to add $(this).serialize() to get all the form fields/values.
untested but should work -
$.post($(this).attr('action'),$(this).serialize(), function(data) {
Hope that helps resolve the issue

Categories