First Name and Message Not shown on body of email - php

i created a contact form php below, for some reason when I submit the entries, the First name shows up on the header but not the Body of the message. Same goes for message. Does anyone know the issue here?
PHP
<?php
$field_name = $_POST['first_name'];
$field_last = $_POST['last_name'];
$field_email = $_POST['cf_email'];
$field_city = $_POST['cf_city'];
$field_state = $_POST['cf_state'];
$field_postal = $_POST['cf_postal'];
$field_country = $_POST['cf_country'];
$field_profession = $_POST['cf_profession'];
$field_industry = $_POST['cf_industry'];
$field_job = $_POST['cf_job'];
$field_linkedin = $_POST['cf_linkedin'];
$field_facebook = $_POST['cf_facebook'];
$field_instagram = $_POST['cf_instagram'];
$field_message = $_POST['cf_message'];
$mail_to = 'membership#name.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'City: '.$field_city."\n";
$body_message .= 'State/Province: '.$field_state."\n";
$body_message .= 'Postal: '.$field_postal."\n";
$body_message .= 'Country: '.$field_country."\n";
$body_message .= 'Profession: '.$field_profession."\n";
$body_message .= 'Industry: '.$field_industry."\n";
$body_message .= 'Job: '.$field_job."\n";
$body_message .= 'LinkedIn: '.$field_linkedin."\n";
$body_message .= 'Facebook: '.$field_facebook."\n";
$body_message .= 'Instagram: '.$field_instagram."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('CourtSide House. Thank you for your submission');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to membership#name.com');
window.location = 'index.html';
</script>
<?php}?>
HTML
<pre><form method="post" action="contact.php">
<p>Contact<p>
<hr>
<div class="row half">
<div class="6u"><input type="text" class="text" placeholder="First Name*" name="first_name"></div>
<div class="6u"><input type="text" class="text" placeholder="Last Name*" name="last_name"></div>
<div class="12u"><input type="text" class="text" placeholder="Email*" name="cf_email"></div>
<div class="4u"><input type="text" class="text" placeholder="City*" name="cf_city"></div>
<div class="4u"><input type="text" class="text" placeholder="State/Province*" name="cf_state"></div>
<div class="4u"><input type="text" class="text" placeholder="Postal Code*" name="cf_postal"></div>
<div class="12u"><input type="text" class="text" placeholder="Country*" name="cf_country"></div>
<div class="4u"><input type="text" class="text" placeholder="Profession*" name="cf_profession"></div>
<div class="4u"><input type="text" class="text" placeholder="Industry*" name="cf_industry"></div>
<div class="4u"><input type="text" class="text" placeholder="Job Title*" name="cf_job"></div>
</div>
<p>Share Some Information About Yourself*<i><small class="small" id="small" style="font-size: 12px">(Please Place Link of Social Media Profile)</small></i></p>
<hr>
<div class="row half">
<div class="4u"><input type="text" class="text" placeholder="LinkedIn Profile*" name="cf_linkedin"></div>
<div class="4u"><input type="text" class="text" placeholder="Facebook Profile*" name="cf_facebook"></div>
<div class="4u"><input type="text" class="text" placeholder="Instagram Profile*" name="cf_instagram"></div>
</div
><div class="row half">
<div class="12u"><textarea name="message" placeholder="Message" ></textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" class="button" value="Submit"></li>
</ul>
</div>
</div>
</form></pre>

You need to append to the variable $body_message instead of overwriting it as you do the second time you call it.
$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";
should be
$body_message = 'From: '.$field_name."\n";
$body_message .= 'From: '.$field_last."\n";

Probably you are confusing your e-mail clients with this:
$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";

1) You're missing the concatenation operator on your variable assignment:
$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";
should be
$body_message = 'From: '.$field_name."\n";
$body_message .= 'From: '.$field_last."\n";
^^^^^
HERE
2) You're message field is just called message:
$field_message = $_POST['cf_message'];
should be
$field_message = $_POST['message'];
Or change
<textarea name="message" placeholder="Message" >
to
<textarea name="cf_message" placeholder="Message" >

Message is not showing because you are using wrong key when retrieving it.
Update with following:
$field_message = $_POST['message']; // Textarea's name is message not cf_message
Then, first name is not showing because you're overwriting $body_message right after you assign $body_message with 'From: '.$field_name."\n";.
Update as follows:
$body_message = 'From: '.$field_name."\n";
$body_message .= 'From: '.$field_last."\n"; // Use .= not just =

Related

Activating Contact Form

I don't know any PHP at all so I researched a couple hours to try and get my contact form to actually save and send the inputs to my email. As of right now, I'm getting an error Failed to load resource: the server responded with a status of 405 (). Here is the contact form code and also PHP file code. They are located in the root directory, named index.html and contact.php. I'm using the template html5up.net/strata.
<form method="post" action="contact.php" id="form" class="contact-form">
<div class="row uniform 50%">
<div class="6u 12u$(xsmall)"><input type="text" name="cf_name" id="name" placeholder="Name" /></div>
<div class="6u$ 12u$(xsmall)"><input type="email" name="cf_email" id="email" placeholder="Email" /></div>
<div class="12u$"><textarea name="cf_message" id="message" placeholder="Message" rows="4"></textarea></div>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="special"/></li>
</ul>
</form>
contact.php
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'simonchangwong#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>

Contact.php cropping away Name

I got a email.php from a website. How do you add an extra field to it, like phone number?
The original one I downloaded works fine.
Email comes as>>
From: Nuski
E-mail: my#email.com
Subject: Test
Message: Test mail
But when I add $field_phone = $_POST['cf_phone']; and $body_message = "Phone: ".$field_phone."\n";
The email comes without the Name of the sender. Like>>
Phone: 5559
E-mail: flex#f.com
Subject: reCaptcha test
Message: Email Test
Here is my HTML form
<form action="email.php" method="post">
<div class="col-sm-6">
<div class="form-group">
<input class="form-control" name="cf_name" type="text" id="name" placeholder="Your Name" required>
</div>
<div class="form-group">
<input class="form-control" name="cf_phone" type="text" id="phone" placeholder="Phone" >
</div>
<div class="form-group">
<input class="form-control" name="cf_email" type="email" id="email" placeholder="Your Email" required>
</div>
<div class="form-group">
<input class="form-control" name="cf_subject" type="text" id="subject" placeholder="Subject" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<textarea class="form-control btn-block" name="cf_message" id="comments" placeholder="Please include your address and item code for delivery" required></textarea><br>
</div>
<div class="form-group">
<input type="submit" class="btn btn-block" id="submit" value="Send Message">
</div>
</div>
<div class="col-sm-12">
<p class="contact-success">Your Message has been Successfully Sent!</p>
<p class="contact-error">Error! Something went wrong!</p>
</div>
</form>
Here is my email.php
<?php
$field_name = $_POST['cf_name'];
$field_phone = $_POST['cf_phone']; //added by me
$field_email = $_POST['cf_email'];
$field_subject = $_POST['cf_subject'];
$field_message = $_POST['cf_message'];
$mail_to = 'ask#aki.co';
$subject = 'Message from AkiYTeeS - '.$field_subject;
$body_message = "From: ".$field_name."\n";
$body_message = "Phone: ".$field_phone."\n"; //added by me
$body_message .= "E-mail: ".$field_email."\n";
$body_message .= "Subject: ".$field_subject."\n";
$body_message .= "Message: ".$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to ');
window.location = 'index.html';
</script>
<?php
}
?>
Replace:
$body_message = "Phone: ".$field_phone."\n"; //added by me
With:
$body_message .= "Phone: ".$field_phone."\n"; //added by me // You forgot a "." here
Also note that it's better to use \r\n unstead of just \r or \n as windows mac and linux each need a different of the 2 so by adding both it should always work
$body_message = "Phone: ".$field_phone."\n";
You have forgotten the concatenation operator in the above line.
use "." (dot)
$body_message .= "Phone: ".$field_phone."\n";

My php Contact form isn't working properly

The contact form will only send an email will the words name email and message. I have been going over it for a few hours but nothing seems to be working.
Here is the contact php file (email is edited)
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'outlook.com';
$subject = 'Message about Tejano Fest '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
Here is the html segment it's related to:
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label>
<input name="author" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label>
<input name="email" type="text" class="input_field" id="email" maxlength="80" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
</form>
Thank you in advance for any assistance.
change it (there are the name of input):
$field_name = $_POST['author'];
$field_email = $_POST['email'];
$field_message = $_POST['text'];
Enjoy your code!
Use this... your POST array values does not matching with your form field names .
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label>
<input name="cf_name" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label>
<input name="cf_email" type="text" class="input_field" id="email" maxlength="80" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="cf_message" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
<?php
if(isset($_POST['submit'])){
$field_name = $_POST['author'];
$field_email = $_POST['email'];
$field_message = $_POST['text'];
$mail_to = 'test#outlook.com';
$subject = 'Message about Tejano Fest '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
} }?>
Some other option to consider(of course after changing the values of the POST arrays), you may want to try using PhpMailer-http://phpmailer.worxware.com/?pg=examplebsendmail as maybe your hosting solution requires Smtp authourisation.

Simple PHP form still not working

Im uploading my website at the moment. However, I do not get my PHP code to work. I do have an mail server set up with the correct e-mail address. But when I use the script and press the button to send the form it gives me a blank page and no e-mail showing in my mailbox.
Could someone help me please ?
Find below my coding:
<div id="thirdColumn">
<div id="contactOns">
<form action="send.php" method="post" class="form">
<p class="name">
<input type="text" name="name" id="name" placeholder="NAAM" />
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="EMAIL" />
</p>
<p class="text">
<textarea type="text" name="message" id="message" placeholder="BERICHT"></textarea>
</p>
<p class="submit">
<input type="submit" id="sent" value="VERSTUUR" />
</p>
</form>
</div> <!-- End contactOns -->
</div> <!-- End thirdColumn -->
And here my PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail_to = 'info#dereebokgrandcafe.nl';
$subject = 'Bericht van een bezoeker '.$name;
$body_message = 'From: '.$name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$message;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
?>
Try:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail_to = 'info#dereebokgrandcafe.nl';
$subject = 'Bericht van een bezoeker '.$name;
$body_message = 'From: '.$name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$message;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
mail($mail_to,$subject,$body_message,$headers);
?>
Unfortunately! You are missing mail function.
mail($mail_to,$subject,$body_message,$headers);
Add this line in the end of your script to test sending mail , and send.
if(mail(mail_to, $subject, $message, $headers)) echo 'Mail sent '; else echo 'problem !';

Code to submit the email form without refreshing the page and without validation

I have created a contact form using requires attribute. Now i want to submit this email form without refreshing the page. When i googled it, I got with the validation code, i don't need validation because i used required attribute in my html. I just want that function which i can add so that the page wont be refreshed after submitting and email should go..
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Contact Form</title>
<link rel="stylesheet" media="screen" href="styles.css">
</head>
<body>
<div id="contact">
<form class="contact_form" action="contact.php" method="post" name="contact_form">
<ul>
<li>
<h2>Contact Us</h2>
<span class="required_notification">* Denotes Required Field</span>
</li>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="John Doe" required />
</li>
<li>
<label for="email">Email:</label>
<input type="email" name="email" id="email" placeholder="john_doe#example.com" required /> <span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea name="message" id="message" cols="40" rows="6" required></textarea>
</li>
<li>
<button class="submit" id="submit_btn" type="submit">Submit Form</button>
</li>
</ul>
</form>
</div>
</body>
</html>
contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'babloopuneeth#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
?>
</script>
<?php
} else { ?>
<?php
}
?>
I dont know what to add inside if and else cases to achieve my objective? Please help me out..
You would have to use jQuery's ajax function to send a form without refreshing the page, here's the official documentation: http://api.jquery.com/jQuery.ajax/
Hope it helps!
Just use this as contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'babloopuneeth#gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
?>

Categories