Check-box Value to be emailed - php

I have a form that allows people to request a free eBook. The form details will be sent to an admin, upon return they will email the eBook to the user. I need to be able to show in this "system" email whether the user gave consent to be manually subscribed to the newsletter or not.
The checkbox is there, checked as the default state.
Here is the system mail I receive after submitting the form:
--
Name: Aniska
Surname: Berry
Email: nissberry#gmail.com
Residential: Ballito
Newsletter:
--
^As you can see the "Newsletter" value is empty - I need to be able to see here whether the user gave consent or not. (Whether the box was checked or not)
Here is my form:
<form method="POST" id="contact-form" class="form-inline" action="contactengine.php" onSubmit="alert('Thank you for your feedback!');">
<div class="form-group white-text">
<label>Name</label>
<input type="text" name="Name" id="Name" class="form-control wow fadeInUp" placeholder="Name" required/>
</div>
<div class="form-group white-text">
<label>Surname</label>
<input type="text" name="Surname" id="Surname" class="form-control wow fadeInUp" placeholder="Surname" required/>
</div>
<div class="form-group white-text m-t">
<label>Email address</label>
<input type="text" name="Email" id="Email" class="form-control wow fadeInUp" placeholder="Email" required/>
</div>
<div class="form-group white-text m-t">
<label>Residential Area</label>
<input type="text" name="Residential" id="Residential" class="form-control wow fadeInUp" placeholder="Residential Area" required/>
</div>
<div class="form-group checkbox white-text">
<label>
<input name="newsletter" type="checkbox" class="checked" checked>Consent
</label>
</div>
<div class="form-group m-t m-b">
<input type="submit" name="submit" value="Please send me my EBook!" class="btn-primary wow fadeInUp" />
</div>
</form>
And here is my php
<?php
$EmailFrom = "admin#webmaster.com";
$EmailTo = "nissberry#gmail.com";
$Subject = "eBook request from User";
$Name = Trim(stripslashes($_POST['Name']));
$Surname = Trim(stripslashes($_POST['Surname']));
$Email = Trim(stripslashes($_POST['Email']));
$Residential = Trim(stripslashes($_POST['Residential']));
$Newsletter = Trim(stripslashes($_POST['Newsletter']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Surname: ";
$Body .= $Surname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Residential: ";
$Body .= $Residential;
$Body .= "\n";
$Body .= "Newsletter: ";
$Body .= $Newsletter;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.remax-panache.co.za\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

You cannot use check boxes that way. Use this way:
$Newsletter = isset($_POST['Newsletter']) ? true : false;
$Newsletter = isset($_POST['Newsletter']) ? "Yes" : "No";
Either of the above should work. When you POST a check box, it posts using this way:
Checked: name=on
Unchecked: (empty) Doesn't post anything.

Related

Error 405 when trying to send an email from a static page

I have a static site up (my portfolio site), with a form to send an email to me using a custom php.
the page is through github pages and the custom domain is through google.
the form is:
<div class="col-sm-12 col-md-6 wow fadeInUp" data-wow-duration="0.8s" data-wow-delay="0.2s">
<form id="contactForm" class="single-form quate-form wow fadeInUp" data-toggle="validator">
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="row">
<div class="col-sm-12">
<input name="name" class="contact-name form-control" id="name" type="text" placeholder="First Name" required>
</div>
<div class="col-sm-12">
<input name="name" class="contact-email form-control" id="L_name" type="text" placeholder="Last Name" required>
</div>
<div class="col-sm-12">
<input name="name" class="contact-subject form-control" id="email" type="email" placeholder="Your Email" required>
</div>
<div class="col-sm-12">
<textarea class="contact-message" id="message" rows="6" placeholder="Your Message" required></textarea>
</div>
<!-- Subject Button -->
<div class="btn-form col-sm-12">
<button type="submit" class="btn btn-fill btn-block" id="form-submit">Send Message</button>
</div>
</div>
</form>
</div>
And the php to send it is:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$EmailTo = "#gmail.com";
$Subject = "Portfolio CV/Resume";
// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "success";
}else{
echo "invalid";
}
?>
The actual error I get is:
POST https://codewithmarcus.com/process.php 405 jquery.min.js:4
I removed my actual email address from the code but rest assured it is in there correctly, any help would be appreciated.
405 http code means Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource.
And you won't be able to send email via Gmail through this script. To send email through Gmail, you need send it through SMPT server. You can use PHPmailer libraryfor that.

HTML Post action sends me to php form

I am a complete novice with PHP, I just want a simple email contact form that will display an error if a required field isn't entered, and will give a thank you message if the email is sent.
When I click my submit button the email does get sent but the website redirects straight to the /send-email.php page, which is blank and useless. The required form fields also don't seem to do anything in the way of preventing an email from being sent if the forms aren't filled.
HTML form code:
<form class="contact-form" action="php/send-email.php" method="post" novalidate>
<div class="row">
<div class="column width-6 pad-1 contact-column">
<h5>Your Name*</h5>
<input type="text" name="name" class="form-box" tabindex="1" required>
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Email*</h5>
<input type="text" name="email" class="form-box" tabindex="2" required>
</div>
</div>
<div class="row mt-2">
<div class="column width-6 pad-1 contact-column">
<h5>Your Website</h5>
<input type="text" name="website" class="form-box" tabindex="3">
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Company</h5>
<input type="text" name="company" class="form-box" tabindex="4">
</div>
</div>
<div class="column width-12">
<input type="text" name="honeypot" class="form-honeypot">
</div>
<div class="row mt-2">
<div class="column width-12 pad-1">
<h5>Your Message*</h5>
<textarea name="message" class="form-text" tabindex="5" required></textarea>
</div>
</div>
<div class="row mt-2">
<input type="submit" value="Send Your Message!" class="btn btn-large bg-blue">
</div>
</form>
Php file
<?php
$recipient = "contact#mysite.com";
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST["website"], FILTER_SANITIZE_URL);
$company = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email.'' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$subject = "New email from contact form";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Website: $website\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n\n\n";
if(mail($recipient, $subject, $email_content, $headers)){
echo "Thanks for the email, we'll get back to as soon as possible!";
}
?>
Any help in the right direction is appreciated!
Check for empty input. Try using if else statement. eg.
if($name == '')
{
echo "Please fill in name";
}
else if($var == '')
{
//error message
}
or you can try
if($name == '' || $email == '' || $var == '')
{
echo "Please fill in all the blank";
}
I'm quite new too to PHP, so this is just a simple checking. Hope it'll helps!

Undefined error in contact Form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I recently created a contact form with php as well but everytime I click submit, I only get an undefined error, nothing else. Can anyone please give an insight into what I missed?
HTML
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="contactengine.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" name="Phone" 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>
PHP
<?php
if (isset($_POST["submit"])){
$EmailFrom = "$Email";
$EmailTo = "ben.afunsho#gmail.com";
$Subject = "New Message from Your Message";
$Name = trim(stripslashes($_POST['Name']));
$Phone = trim(stripslashes($_POST['Phone']));
$Email = trim(stripslashes($_POST['Email']));
$Subject = trim(stripslashes($_POST['Subject']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK = true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "You have received a new email from";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= "$Subject: ";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message; $Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}
?>
I only get an undefined error on the html nothing else
There were some case sensitivity issues in your post and I corrected it and commented at corresponding lines.
Check it
<?php
if (isset($_POST["submit"]))
{ $EmailFrom = "$Email";
$EmailTo = "ben.afunsho#gmail.com";
$Subject = "New Message from Your Message";
$Name = trim(stripslashes($_POST['Name']));
$Phone = trim(stripslashes($_POST['Phone']));
$Email = trim(stripslashes($_POST['Email']));
$Subject = trim(stripslashes($_POST['subject'])); //s should be small
$Message = trim(stripslashes($_POST['message'])); // m should be small
// validation
$validationOK = true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit;
}
// prepare email body text
$Body = "You have received a new email from";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject; // copy paste is very bad
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page i
if ($success) { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; }
else { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; }}?>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="contactengine.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" name="Phone" 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">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Submit" />
</div>
</div>
</form>

Troubleshooting php form submition blank fields in emails

I know this question has been asked before, I have researched but can't seem to get to the route of the problem. I'm hoping someone can help. Ok so form seems to be working in as much as the form has to be completed before it allows to be sent, the form sends and an email arrives. Unfortunately the email fields are blank.
So here's the php:
<?php
$Body = 'MIME-Version: 1.0' . "\r\n";
$Body .= 'Content-type: text/html; ch***t=utf-8' . "\r\n";
$Body .= 'From: '. $EmailFrom . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$EmailFrom = "info#mywebsite.com";
$EmailTo = "info#mywebsite.com";
$Subject = "Contact Form";
$Name = trim(stripslashes($_POST['Name']));
$Email = trim(stripslashes($_POST['Email']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://mywebsite.com/formredirect.html");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom" . "\r\n");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=formredirect.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=error.html\">";
}
?>
My HTML:
<form method="post" action="contact.php" name="contact" id="contact">
<div class="row 50%">
<div class="6u 12u(mobile)">
<input type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="6u 12u(mobile)">
<input type="text" name="email" id="email" placeholder="Email" required />
</div>
</div>
<div class="row 50%">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" required></textarea>
</div>
</div>
<div class="row 50%">
<div class="12u">
<input type="submit" class="button" id="submit" value="submit"></a>
</div>
</div>
</form>
I appreciate it's probably me being really dense. I'm new to this so am fumbling my way around. Any help would be greatly appreciated. Many thanks!
The $_POST keys are CaSe SenSitIve
$_POST['Name'] is different from $_POST['name']
Either change the form name attribute or the POST keys so they match, i.e.:
FORM
<input type="text" name="name" id="name" placeholder="Name" required />
...
<input type="text" name="email" id="email" placeholder="Email" required />
...
<textarea name="message" id="message" placeholder="Message" required></textarea>
...
PHP
...
$Name = trim(stripslashes($_POST['name']));
$Email = trim(stripslashes($_POST['email']));
$Message = trim(stripslashes($_POST['message']));
...

Is there anything wrong with the code in my form?

I'm using this tutorial to create a contact form for my site and it doesn't seem to be working. After I fill in my form and press submit, nothing happens. I would like to know whats wrong with it and how I can fix it.
This is my code:
HTML:
<form action="mail/contact.php" method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name:</label><br />
<input type="text" name="name" id="name" class="form-control" placeholder="Name"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email:</label><br />
<input type="text" name="email" id="email" class="form-control" placeholder="Email"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message:</label>
<textarea name="message" rows="5" id="message" class="form-control" placeholder="Message"></textarea>
</div></div>
<br />
<input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg"/>
</form>
PHP:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'name#mail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
It's not a problem actually, you hace to change $to to $email in the email() method because the variable that has the email you want to send to is $email.
Your contact.php code will be:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($email, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
Another recommendation is to check this tutorial for a secure contact form.
Also you should check out this two php frameworks:
Symfony 2 Mail
Zend 2 Mail

Categories