Post Data is not coming - php

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

Related

How to create a pop up contact form that return success message in the place of the send button?

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>

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

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" >

resetting form after php submit

I am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>
Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!

Simple Php Mailer.php and a contact.html, need html help might be missing few tags

Hi im totally new to php, i have a website where i have a contact.html,it has forms where in name, email, insurance type, subject and comments can be filled.
Up on clicking submit button, the specfied email will recieve the details. Cant seem to locate the problem.
here is the html code of contact.html
<form id="ajax-contact-form">
<div class="row">
<div class="span4">
<div class="form-padding">
<label for="f1">Your name(required):</label>
<input type="text" name="name" id="f1" class="form-text" value=""/>
<label for="f2">Insurance type:</label>
<input type="text" name="category" id="f2" class="form-text" value="" size="40"/>
</div>
</div>
<div class="span4">
<div class="form-padding">
<label for="f3">Your email(required):</label>
<input type="text" name="email" id="f3" class="form-text" value="" size="40"/>
<label for="f4">Subject:</label>
<input type="text" name="subject" id="f4" class="form-text" value="" size="40"/>
</div>
</div>
</div>
<div class="row">
<div class="span8">
<div class="form-padding">
<label for="f5">Message(required):</label>
<textarea name="message" id="f5" cols="40" rows="10"></textarea>
</div>
</div>
</div>
<!-- send mail configuration -->
<input type="hidden" value="myemail#gmail.com" name="to" id="to" />
<input type="hidden" value="myemail#gmail.com" name="from" id="from" />
<input type="hidden" value="From contact form" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" class="button red small" value="Submit Form" name="submit" id="submit" /></p>
</form>
AND MY SEND-MAIL.PHP
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['f1'] ."<br>\n";
$msg .= "INSURANCE: " .$_POST['f2'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['f3'] ."<br>\n";
$msg .= "SUBJECT: " .$_POST['f4'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['f5'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
My JS function
// contact form
$("#ajax-contact-form").submit(function() {
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "send-mail.php",
data: str,
success: function(msg) {
if(msg == 1) {
result = '<div class="alert success fade in">Your message has been sent. Thank you!</div>';
$("#ajax-contact-form").hide();
} else {result = msg;}
$('#form-message').hide();
$('#form-message').html(result);
$('#form-message').fadeIn("slow");
}
});
return false;
});
My Questions
There seems to be an error, cant send mails. Im a naive. Any help would be appreciated.
How to do i show a success message after clicking submit button.
Thanks
maybe you have to complete your form open tag
<form id="ajax-contact-form" method="POST" action="send-mail.php">
unless you using Ajax, you have to provide the ajax code in your question.

Categories