PHP Form only emails 1 value - php

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?

Related

Why isn't my PHP form working? I do receive the email but there is no content in it

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.

Contact us form: Mail not received [duplicate]

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

Why is my php script not send the contact form to my email?

I tried creating a simple PHP script to send an email to me when someone fills out a contact form on my website but I get nan error that says:
inspirehealth.today is currently unable to handle this request.
HTTP ERROR 500
<?php
$first_name = $_POST('first_name');
$last_name = $_POST('last_name');
$email = $_POST('email');
$message = $_POST('message');
$to = "lewkowicz613#gmail.com";
$subject = "Message from Inspire Health";
mail ($to, $subject, $message, "From: " . $first_name . $last_name);
echo "Your Message Has Been Sent";
?>
and here is my html file relevant to the form
<section class="section-form js--contact" id="contact">
<div class="row">
<h3>Please Subscribe to Our Email List!</h3>
</div>
<div class="row">
<form method="post" action="form_process.php" class="contact-form">
<div class="row">
<div class="col span-1-of-3">
<label for="first_name">First Name</label>
</div>
<div class="col span-2-of-3">
<input type="text" name="first_name" id="first_name" placeholder="First Name" required>
</div>
</div>
<div class="row">
<div class="col span-1-of-3">
<label for="last_name">Last Name</label>
</div>
<div class="col span-2-of-3">
<input type="text" name="last_name" id="last_name" placeholder="Last name" required>
</div>
</div>
<div class="row">
<div class="col span-1-of-3">
<label for="email">Email</label>
</div>
<div class="col span-2-of-3">
<input type="email" name="email" id="email" placeholder="Your email" required>
</div>
</div>
<div class="row">
<div class="col span-1-of-3">
<label for="message">Drop us a line</label>
</div>
<div class="col span-2-of-3">
<textarea name="message" id="message" placeholder="Your message" ></textarea>
</div>
</div>
<div class="row">
<div class="col span-1-of-3">
<label> </label>
</div>
<div class="col span-2-of-3">
<input type="submit" value="Send it!">
</div>
</div>
</form>
</div>
</section>
Im not sure why its not working properly. Is it a server thing because I uploaded all the files to my domain root directory.
Thanks a lot.
$_POST is a global array, so try:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "lewkowicz613#gmail.com";
$subject = "Message from Inspire Health";
mail ($to, $subject, $message, "From: " . $first_name . $last_name);
echo "Your Message Has Been Sent";
?>
Hope this helps :)

HTML+PHP form not sending to my email

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);
}
?>

Form shows success message but no email is received

I have a Bootstrap 3 form that displays the success message: "Your message has been sent" but I don't receive any emails. Not even in my spam/junk folder. It validates properly but no email is sent. Here's the PHP code. Please help!
PHP
<?php
// check if fields passed are empty
if(empty($_POST['firstname']) ||
empty($_POST['lastname']) ||
empty($_POST['address']) ||
empty($_POST['address2']) ||
empty($_POST['city']) ||
empty($_POST['state_province']) ||
empty($_POST['country']) ||
empty($_POST['zip']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
empty($_POST['checkbox']) ||
empty($_POST['comment']) ||
{
echo "No arguments Provided!";
return false;
}
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state_province = $_POST['state_province'];
$country = $_POST['country'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$checkbox = $_POST['checkbox'];
$message = $_POST['comment'];
// create email body and send it
$to = 'email#example.com'; // put your email
$email_subject = "Contact form submitted by: $firstname";
$email_body = "You have received a new message. \n\n".
"Here are the details:\n \nFirst Name: $firstname \n Last Name: $lastname\n Address: $address\n Address2: $address2\n City: $city\n State/Province: $state_province\n Country: $country\n zip: $zip\n Phone: $phone\n Email: $email_address\n Checkbox: $checkbox\n Message: \n $message";
$headers = "From: $email_address\n";
$headers = "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
HTML
<form class="form-horizontal" role="form" id="ContactUs" novalidate>
<!-- First Name -->
<div class="form-group">
<label class="control-label col-md-4" for="firstname"><abbr title="Required field">*</abbr>First Name</label>
<div class="col-md-8">
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="First Name" required data-validation-required-message="Please enter your first name">
<div class="help-block"></div>
</div>
</div>
<!-- Last Name -->
<div class="form-group">
<label class="control-label col-md-4" for="lastname"><abbr title="Required field">*</abbr>Last Name</label>
<div class="col-md-8">
<input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last Name" required data-validation-required-message="Please enter your last name">
<div class="help-block"></div>
</div>
</div>
<!-- Address -->
<div class="form-group">
<label class="control-label col-md-4" for="address"><abbr title="Required field">*</abbr>Address</label>
<div class="col-md-8">
<input type="text" class="form-control" id="address" name="address" placeholder="Address" required data-validation-required-message="Please enter your address">
<div class="help-block"></div>
</div>
</div>
<!-- Address 2 -->
<div class="form-group">
<label class="control-label col-md-4" for="address2">Address 2</label>
<div class="col-md-8">
<input type="text" class="form-control" id="address2" name="address2" placeholder="Address 2">
</div>
</div>
<!-- City -->
<div class="form-group">
<label class="control-label col-md-4" for="city"><abbr title="Required field">*</abbr>City</label>
<div class="col-md-8">
<input type="text" class="form-control" id="city" name="city" placeholder="City" required data-validation-required-message="Please enter your city">
<div class="help-block"></div>
</div>
</div>
<!-- State/Province -->
<div class="form-group">
<label class="control-label col-md-4" for="state_province"><abbr title="Required field">*</abbr>State/Province</label>
<div class="col-md-8">
<input type="text" class="form-control" id="state_province" name="state_province" placeholder="State/Province" required data-validation-required-message="Please enter your State or Province">
<div class="help-block"></div>
</div>
</div>
<!-- Country -->
<div class="form-group">
<label class="control-label col-md-4" for="country"><abbr title="Required field">*</abbr>Country</label>
<div class="col-md-8">
<input type="text" class="form-control" id="country" name="country" placeholder="Country" required data-validation-required-message="Please enter your Country of residence">
<div class="help-block"></div>
</div>
</div>
<!-- Zip/Postal Code -->
<div class="form-group">
<label class="control-label col-md-4" for="zip"><abbr title="Required field">*</abbr>Zip/Postal Code</label>
<div class="col-md-8">
<input type="text" class="form-control" id="zip" name="zip" placeholder="Zip/Postal Code" required data-validation-required-message="Please enter your Zip or Postal Code">
<div class="help-block"></div>
</div>
</div>
<!-- Phone -->
<div class="form-group">
<label class="control-label col-md-4" for="phone">Phone</label>
<div class="col-md-8">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone">
</div>
</div>
<!-- Email Address -->
<div class="form-group">
<label class="control-label col-md-4" for="email"><abbr title="Required field">*</abbr>Email address</label>
<div class="col-md-8">
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address" required data-validation-required-message="Please enter your email">
<div class="help-block"></div>
</div>
</div>
<!-- Mailing List -->
<div class="form-group">
<label class="control-label col-md-4" for="checkbox">Include in mailing list</label>
<div class="col-md-8">
<input type="checkbox" class="form-control" id="checkbox" name="checkbox" value="1">
</div>
</div>
<!-- Comment -->
<div class="form-group">
<label class="control-label col-md-4" for="comment">Comment</label>
<div class="col-md-8">
<textarea class="form-control" id="comment" name="comment" rows="3"></textarea>
</div>
</div>
<!-- Buttons -->
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<div id="success"> </div> <!-- For success/fail messages -->
<button type="submit" class="btn btn-primary pull-right">Submit</button>
</div>
</div>
</form>
UPDATE 8:50PM 03/09
I uploaded this to a different host and now, when I submit the form I'm getting an error :"Message cannot be sent!"
you are sending the email to "email#example.com"
Probably try sending it to your email address, by changing the $to variable.
Also change the headers. From This
$headers = "From: $email_address\n";
$headers = "Reply-To: $email_address";
To:
$headers = "From: $email_address\n";
$headers .= "Reply-To: $email_address";
note the .= on the second line, this means add the string onto headers vs replacing the content of the variable.
And maybe change this:
mail($to,$email_subject,$email_body,$headers);
return true;
to
return mail($to,$email_subject,$email_body,$headers); // will return true on success send, or false if it failed to send.

Categories