Lots of similar questions have been asked, but couldn't find an answer to the exact problem I have. I have a form that works completely fine, but I asked someone to design it nicer (which he did) and now it just doesn't work anymore. I don't know what's causing it.
I'm assuming there is something wrong in my HTML code for the form, since the .php page works perfectly fine (I copy/pasted it entirely) on my previous page.
Any idea?
<form name="htmlform" method="post" action="html_form_send.php">
<div class="row has-form">
<div class="col-xs-12 col-sm-8 col-sm-offset-2">
<div class="form-group">
<label for="first_name" class="col-sm-3 control-label">Prénom *</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="first_name">
</div>
</div>
<div class="form-group">
<label for="last_name" class="col-sm-3 control-label">Nom *</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="last_name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-3 control-label">Email *</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-3 control-label">Téléphone</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="telephone">
</div>
</div>
<div class="form-group">
<label for="comments" class="col-sm-3 control-label">Votre critique *</label>
<div class="col-sm-9">
<textarea class="form-control" rows="6" id="comments"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-9 col-sm-offset-3 text-center">
<button type="submit" class="btn btn-primary btn-lg btn-block">Envoyer</button>
</div>
</div>
</div>
</form>
your inputs have no "name" attribute add a "name" attribute with the same value as the "id" attribute to each input tag as
Php uses NAME as the identifier when posted.
To Clearify:
if you try and get an input value from the POST in PHP you have to use the following code:
$var = $_POST['IDENTIFIER'];
Where IDENTIFIER is the value of the "name" attribute of the INPUT or TEXTAREA or any other HTML FORM element you're trying to retrieve.
all your inputs need attribute name="first_name" and etc:
<input type="text" class="form-control" id="first_name" name="first_name">
html_form_send.php must looks like:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
//preparing mail data
$from = $email;
$to = "yourmail#exemple.com";
$subject = "Sending Form From site";
$message = "First name: $first_name \r\nLast name:$last_name \r\nTelephone: $telephone \r\nComments: $comments";
$headers = 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
//sending
mail($to, $subject, $message, $headers);
}
?>
Related
PHP form on my website isn't responding well. It would be appreciated if someone could help me.
This is the HTML Code of the form
<form action="contactform.php" method="post" name="form" class="p-5 bg-white">
<div class="row form-group">
<div class="col-md-6 mb-3 mb-md-0">
<label class="text-black" for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-md-6">
<label class="text-black" for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="email">Email</label>
<input type="email" id="email" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="subject">Subject</label>
<input type="subject" id="subject" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="mssg">Message</label>
<textarea name="mssg" id="mssg" cols="30" rows="7" class="form-control" placeholder="Write your notes or questions here..."></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary py-2 px-4 text-white">
</div>
</div>
</form>
This is the PHP Code
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "alyyashar#gmail.com";
$subject = "New email from your site!";
$fname = $_POST['fname'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$title = '<h3>Hello! You have received a new mail from your website!</h3>';
$body = "$title
<br/>
<b>From:</b> $fname
<br/>
<b>E-Mail:</b> $email
<br/>
<b>Message:</b>\n$message
<br/>
<br/>";
if (mail($to, $subject, $body, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$fname.", We will contact you shortly!</h1>";
} else {
echo "Something went wrong!";
}
}
?>
<br>
Back to Homepage
This is the email I receive
Screenshot of the email
When I enter information into the form and click send message, I do receive the email but there is no content in it.
The form elements have no name attributes, which is what the browser uses to send their name/value pairs to the server. So while it's posting the form, none of the values are being included.
For example, this:
<input type="text" id="fname" class="form-control">
Should be this:
<input type="text" id="fname" class="form-control" name="fname">
The same fix would need to be repeated for the remaining form elements.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm trying to create a contact us form with PHP. The problem is that, I'm not receiving the email in my account. When the submit button is clicked it takes me to the mail.php page where a "thank you" message is shown.
The code is as shown below:
<div class="container">
<div class="col-xs-offset-2 col-xs-10 col-sm-8">
<h2>Contact Us</h2>
<form action="mail.php" method="POST" class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2 col-xs-6" for="email">First Name:</label>
<div class="col-sm-6 col-xs-6">
<input type="text" class="form-control" id="fname" placeholder="First Name*" name="fname" required="required">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Last Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="lname" placeholder="last Name" name="lname" >
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email ID:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="email" placeholder="Email Address*" name="email" required="required">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="message">Message:</label>
<div class="col-sm-6">
<textarea id="form_message" name="message" class="form-control" placeholder="Message*" rows="4" required="required" data-error="Please, leave us a message."></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
The code for mail.php:
<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $fname $lname \n Message: $message";
$recipient = "rubeena.ajeed#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Please try using SMTP ?
PHPmailler.class
Download : https://github.com/PHPMailer/PHPMailer
Trying to send an email not displaying any error message or anything just refreshing the page that it.
I am trying an email through contact form but its not working.Here is the code for that. tried by doing echo but it is not working .not displaying any error as well.
contact and contactus:
<?php ob_start();
if(isset($_POST['submit'])) {
$to = "abc#gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$headers = "From: " .$email . "\r\n" .
"CC: xyz#gmail.com";
mail($to,$subject,$txt,$headers);
header("Location: contact.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>HMS System</title>
</head>
<body>
<div class="container">
<div class="page-header">
<h2>Contact Us Form</h2>
</div>
<form class="form-horizontal" action="contactus.php" method="post" role="form">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="name" placeholder="Insert your Name" id="name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email Address</label>
<div class="col-sm-8">
<input type="email" class="form-control" name="email" placeholder="Email Address" id="email">
</div>
</div>
<div class="form-group">
<label for="subject" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="subject" placeholder="Subject" id="subject">
</div>
</div>
<div class="form-group">
<label for="comments" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-8">
<textarea class="form-control" rows="10" name="comment" style="resize:none;"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-8">
<input type="submit" value="Send Message" name="submit_contact" class="btn btn-block btn-danger" id="subject">
</div>
</div>
</form>
</section>
</article>
</div>
<div style="width:50px;height:50px;"></div>
</body>
use this
if(isset($_POST['submit_contact']))
If you are doing it from your local server, you will have to setup your mail properly in ini then only you will be able to send mail from your local machine.
A better approach would be to wrap the mail function in a statement. Mail is returning a bool value. And beginn as simple as possible.
error_reporting (E_ALL);
$to = "mail#whatever.com"
// ... your settings ...
if ( mail ( $to , $subject , $message ) ) {
// do when ok
}
else {
// do when error
}
To verify that you receive all values from $_POST do
echo '<pre>';
print_r ($_POST);
echo '</pre>';
When I submit my html form I am only getting the $name value in my email. Also when I click submit instead of redirecting to my thank you html page the content of the thank you page appears underneath the form. Any ideas why? Here is my code...
HTML:
<section class="container content">
<div class="row">
<div class="center title col-sm-12">
<h2>Short Application Form</h2>
</div>
</div>
<h2>Personal Information</h2>
<form method="post" action="appemail.php" name="contactform" id="contactform" class="row">
<fieldset>
<div class="form-field col-sm-6">
<label for="name">First Name</label>
<span><input type="text" name="name" id="name"/></span>
</div>
<div class="form-field col-sm-6">
<label for="lastname">Last Name</label>
<span><input type="text" name="lastname" id="lastname"/></span>
</div>
<div class="form-field col-sm-6">
<label for="email">Email</label>
<span><input type="text" name="email" id="email"/></span>
</div>
<!--<div class="form-field col-sm-6">
<label for="email">Referral Name</label>
<span><input type="email" name="email" id="email"/></span>
</div>-->
<div class="form-field col-sm-6">
<label for="email">Home/Cell Phone</label>
<span><input type="text" name="phone" id="phone"/></span>
</div>
<div class="form-field col-sm-6">
<label>Martial Satus</label>
<select name="martial">
<option>Married</option>
<option>Unmaried</option>
<option>Seperated</option>
</select>
</div>
<div class="form-field col-sm-6">
<label>Are you a Canadian Permanent Resident</label>
<select name="resident">
<option>Yes</option>
<option>No</option>
</select>
</div>
<div class="form-field col-sm-6">
<label for="email">Date of Birth</label>
<span><input type="text" name="birth"/></span>
</div>
<div class="form-field col-sm-6">
<label for="email">Number of Dependents</label>
<span><input type="text" name="dependents"/></span>
</div>
<div class="center title col-sm-12">
<h2 style="text-align:left">Current Address</h2>
</div>
<div class="form-field col-sm-6">
<label for="name">Street Address</label>
<span><input type="text" name="adress"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">City</label>
<span><input type="text" name="city"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Province</label>
<span><input type="text" name="province"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Postal Code</label>
<span><input type="text" name="postal"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Years at this Address</label>
<span><input type="text" name="years"/></span>
</div>
<div class="form-field col-sm-6">
<label>Rent or Own</label>
<select name="rent">
<option>Own</option>
<option>Rent</option>
</select>
</div>
<div class="center title col-sm-12">
<h2 style="text-align:left">Subject Property</h2>
</div>
<div class="form-field col-sm-6">
<label>Property Usage</label>
<select name="property">
<option>Primary Residence</option>
<option>Second Home</option>
<option>Investment</option>
</select>
</div>
<div class="form-field col-sm-6">
<label for="name">Loan Amount $ </label>
<span><input type="text" name="loan"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Purchase Price $</label>
<span><input type="text" name="price"/></span>
</div>
<div class="center title col-sm-12">
<h2 style="text-align:left">Monthly Income</h2>
</div>
<div class="form-field col-sm-6">
<label for="name">Yearly Income $ </label>
<span><input type="text" name="yearly"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Other Sources of Income (combined) $</label>
<span><input type="text" name="other"/></span>
</div>
<div class="form-field col-sm-12">
<label for="message">Comments</label>
<span><textarea name="message" id="message"></textarea></span>
</div>
</fieldset>
<div class="form-click center col-sm-12">
<span><input type="submit" name="submit" id="submit" value="Send it"/></span>
</div>
<div id="alert" class="col-sm-12"></div>
</form>
</section>
PHP:
<?php
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$martial = $_POST['martial'];
$resident = $_POST['resident'];
$birth = $_POST['birth'];
$dependents = $_POST['dependents'];
$adress = $_POST['adress'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$years = $_POST['years'];
$rent = $_POST['rent'];
$property = $_POST['property'];
$loan = $_POST['loan'];
$price = $_POST['price'];
$other = $_POST['other'];
$message = $_POST['message'];
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = $visitor_email;
$email_subject = "Application Loan";
$email_body = "You have received a new message from $name\n\n".
"Here is the Application: \n\n".
"Phone: $phone\n\n".
"Martial Status: $martial\n\n".
"Resident: $resident\n\n".
"Birth: $birth\n\n".
"Dependents: $dependents\n\n".
"Address: $address\n\n".
"City: $city\n\n".
"Province: $province\n\n".
"Postal: $postal\n\n".
"Years at this Address: $years\n\n".
"Rent or Own: $rent\n\n".
"Property: $property\n\n".
"Loan: $loan\n\n".
"Price: $price\n\n".
"Other: $other\n\n".
"Message: $message\n\n";
$to = "email#gmail.com";//<== update the email address
$headers = "From: $visitor_email \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location:thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)','(\r+)','(\t+)','(%0A+)','(%0D+)','(%08+)','(%09+)' );
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)){return true;}
else{return false;}
}
?>
Thanks in advance.
The string concatenation doesn't work because of the newline.
rewrite your code and use <br> except \n & when submit form check data using isset function, add a hidden field for doing it.
I've copied over your code and it runs fine, except you have a typo with $address.
Sometimes you use address (which is the correct), and sometimes adress.
The email I received:
You have received a new message from First Name
Here is the Application:
Phone: 0000000
Martial Status: Married
Resident: Yes
Birth: 2014-10-19
Dependents: 42
Address: Street Address
City: City
Province: Province
Postal: Postal Code
Years at this Address: 42
Rent or Own: Own
Property: Primary Residence
Loan: 42
Price: 42
Other: 42
Message: Comments
The new display of lines will work with most email clients because there are no html tags, so it will be displayed as plain text. Setting the appropriate header also helps.
Otherwise, what is your back-end?
OS, host, PHP versions?
This bootstrap using html/php contact form sends me an email just fine, but without any of the users input information!
The email I received looks empty like this:
Name:
Email:
Subject:
Message:
The html for the form is:
<section id="partner">
<div class="container">
<div class="center wow fadeInDown">
<h2><br>Contact</h2>
<p class="lead">Send a general enquiry here or order your service here.</p>
</div>
<div class="row contact-wrap">
<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="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>
</form>
</div><!--/.row-->
</div><!--/.container-->
</section><!--/#contact-page-->
The PHP for the form looks like this:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$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 = 'heerschapnikki#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.'>');
echo json_encode($status);
die;
Why isn't it working properly? The message "thank you for contacting us" pops up fine and the email sends through, but non of the users information comes through.
Try to print the values you are sending. So use this:
var_dump( $_POST['name'] );
var_dump( $_POST['email'] );
etc...
Doing this, you will be able to see wether your fields are even posted correctly.
the fields have no id param. in the post you get the "id" nor the "name"
<label>Name *</label>
<input type="text" id="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" id="email" class="form-control" required="required">