Can we subscribe using MailChimp through localhost? - php

I am doing this first time. I want to ask, can I subscribe using MailChimp through my local server because I am having problem in doing that every time. When I subscribe I do not receive any mail. Here is the code
<?php
// change this path if the class file isn't in the same directory!
include_once 'MailChimp.php';
$alertclass = 'alert-warning';
$msg = '';
$name = '';
$email = '';
if (isset($POST['Submit'])) {
if (empty($_POST['name']) || empty($_POST['email'])) {
$msg = 'Please enter a name and email address.';
} else {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
/*
* Place here your validation and other code you're using to process your contact form.
*/
$mc = new \Drewm\MailChimp('xxxxxxxxx-us11');
$mvars = array('optin_ip'=> $_SERVER['REMOTE_ADDR'], 'FNAME' => $name);
$result = $mc->call('lists/subscribe', array(
'id' => 'f660c6ba5f',
'email' => array('email'=>$email),
'merge_vars' => $mvars,
'double_optin' => true,
'update_existing' => false,
'replace_interests' => false,
'send_welcome' => false
)
);
if (!empty($result['euid'])) {
$msg = 'Thanks, please check your mailbox and confirm the subscription.';
$alertclass = 'alert-success';
} else {
if (isset($result['status'])) {
switch ($result['code']) {
case 214:
$msg = 'You\'re already a member of this list.';
break;
// check the MailChimp API if you like to add more options
default:
$msg = 'An unknown error occurred.';
$alertclass = 'alert-error';
break;
}
}$msg="asdasdad";
}
}
}
?>
I am very new to this.
html code
<form class="form-horizontal"action="contactform-mailchimp.php" method="post">
<?php
//if ($msg != '')
echo '
<div class="alert '.$alertclass.'" role="alert">'.$msg.'</div>';
?>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">First name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" id="inputName">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="email" id="inputEmail">
</div>
</div>
<div class="form-group">
<label for="inputMessage" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" name="message" id="inputMessage"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" name="newsletter"> Subscribe to newsletter
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="Submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>

Related

Boostrapious contact form - how to add some checkbox inputs to form

I use this contact form by Boostrapious and works well, but I can't find solution to add some checkbox inputs and send values by email. I added HTML inputs, but I need help with php code. Can anyone help me?
This is a link to this tutorial and code:
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"> </div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Firstname *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Lastname *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group checkbox">
<label for="checkboxoptions1">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions1" value="checkboxoptions1">
Option1
</label>
<label for="checkboxoptions2">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions2" value="checkboxoptions2">
Option2
</label>
<label for="checkboxoptions3">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions3" value="checkboxoptions3">
Option3
</label>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required. Contact form template by
Bootstrapious.</p>
</div>
</div>
</div>
</form>
and php code here
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'Demo contact form <demo#domain.com>';
// an email address that will receive the email with the output of the form
$sendTo = 'Demo contact form <demo#domain.com>';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('checkboxoptions' => 'My options','name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the necessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $_POST['email'],
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];

Contact form isn't sending the message

so I am building a contact for mon my website, but for some reasons, I am getting this error
[![error image][1]][1] (https://prnt.sc/vw49gd - link to image as the one uploaded doesn't seem to get it)
My HTML form is fairly simple:
<form class="nk-form-submit" action="form/contact.php" method="post">
<div class="row">
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.8">
<label class="field-label textSubTittleColors ttu">Your Name</label>
<div class="field-wrap">
<input name="contact-name" placeholder="First & Last Name" type="text" class="input-bordered contactUsTextfields required">
</div>
</div>
</div>
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.9">
<label class="field-label textSubTittleColors ttu">Your Email</label>
<div class="field-wrap">
<input name="contact-email" placeholder="example#gmail.com" type="email" class="input-bordered contactUsTextfields required email">
</div>
</div>
</div>
</div>
<div class="field-item animated" data-animate="fadeInUp" data-delay="1.0">
<label class="field-label textSubTittleColors ttu">Your Message</label>
<div class="field-wrap ">
<textarea name="contact-message" placeholder="Leave your question or comment here" class="input-bordered contactUsTextfields input-textarea required"></textarea>
</div>
</div>
<input type="text" class="d-none" name="form-anti-honeypot" value="">
<div class="row">
<div class="col-sm-5 text-right animated" data-animate="fadeInUp" data-delay="1.1">
<button type="submit" name="submit" class="btn sendButton" >SEND</button>
</div>
<div class="col-sm-7 order-sm-first">
<div class="form-results"></div>
</div>
</div>
</form>
But the issue relies on the contact.php code:
<?php
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php'); // Include PHPMailer
$mail = new PHPMailer();
$emailTO = $emailBCC = $emailCC = array(); $formEmail = '';
### Enter Your Sitename
$sitename = 'Organization';
### Enter your email addresses: #required
$emailTO[] = array( 'email' => 'myname#microsoftMail.org', 'name' => 'name' );
### Enable bellow parameters & update your BCC email if require.
//$emailBCC[] = array( 'email' => 'email#yoursite.com', 'name' => 'Your Name' );
### Enable bellow parameters & update your CC email if require.
//$emailCC[] = array( 'email' => 'email#yoursite.com', 'name' => 'Your Name' );
### Enter Email Subject
$subject = "Contact Us " . ' - ' . $sitename;
### If your did not recive email after submit form please enable below line and must change to your correct domain name. eg. noreply#example.com
//$formEmail = 'noreply#yoursite.com';
### Success Messages
$msg_success = "We have <strong>successfully</strong> received your message. We'll get back to you soon.";
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST["contact-email"]) && $_POST["contact-email"] != '' && isset($_POST["contact-name"]) && $_POST["contact-name"] != '') {
### Form Fields
$cf_email = $_POST["contact-email"];
$cf_name = $_POST["contact-name"];
$cf_message = isset($_POST["contact-message"]) ? $_POST["contact-message"] : '';
$honeypot = isset($_POST["form-anti-honeypot"]) ? $_POST["form-anti-honeypot"] : 'bot';
$bodymsg = '';
if ($honeypot == '' && !(empty($emailTO))) {
### If you want use SMTP
// $mail->isSMTP();
// $mail->SMTPDebug = 0;
// $mail->Host = 'smtp_host';
// $mail->Port = 587;
// $mail->SMTPAuth = true;
// $mail->Username = 'smtp_username';
// $mail->Password = 'smtp_password';
### Regular email configure
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->From = ($formEmail !='') ? $formEmail : $cf_email;
$mail->FromName = $cf_name . ' - ' . $sitename;
$mail->AddReplyTo($cf_email, $cf_name);
$mail->Subject = $subject;
foreach( $emailTO as $to ) {
$mail->AddAddress( $to['email'] , $to['name'] );
}
### if CC found
if (!empty($emailCC)) {
foreach( $emailCC as $cc ) {
$mail->AddCC( $cc['email'] , $cc['name'] );
}
}
### if BCC found
if (!empty($emailBCC)) {
foreach( $emailBCC as $bcc ) {
$mail->AddBCC( $bcc['email'] , $bcc['name'] );
}
}
### Include Form Fields into Body Message
$bodymsg .= isset($cf_name) ? "Contact Name: $cf_name<br><br>" : '';
$bodymsg .= isset($cf_email) ? "Contact Email: $cf_email<br><br>" : '';
$bodymsg .= isset($cf_message) ? "Message: $cf_message<br><br>" : '';
$bodymsg .= $_SERVER['HTTP_REFERER'] ? '<br>---<br><br>This email was sent from: ' . $_SERVER['HTTP_REFERER'] : '';
// Mailing
$mail->MsgHTML( $bodymsg );
$is_emailed = $mail->Send();
if( $is_emailed === true ) {
$response = array ('result' => "success", 'message' => $msg_success);
} else {
$response = array ('result' => "error", 'message' => $mail->ErrorInfo);
}
echo json_encode($response);
} else {
echo json_encode(array ('result' => "error", 'message' => "Bot <strong>Detected</strong>.! Clean yourself Botster.!"));
}
} else {
echo json_encode(array ('result' => "error", 'message' => "Please <strong>Fill up</strong> all required fields and try again."));
}
}
I have uploaded this on the hostgator domain, but every time I click the send button I keep getting the error mentioned above.
I also tried several different codes from either other posts here in stackoverflow or from youtube videos I have seen, but its all the same result.
[1]: https://i.stack.imgur.com/MQQxS.png
You may have used the wrong HTTP request type. Try the method attribute as shown below.
method="get"
As used in:
Change your HTML to:
<form class="nk-form-submit" action="form/contact.php" method="get">
<div class="row">
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.8">
<label class="field-label textSubTittleColors ttu">Your Name</label>
<div class="field-wrap">
<input name="contact-name" placeholder="First & Last Name" type="text" class="input-bordered contactUsTextfields required">
</div>
</div>
</div>
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.9">
<label class="field-label textSubTittleColors ttu">Your Email</label>
<div class="field-wrap">
<input name="contact-email" placeholder="example#gmail.com" type="email" class="input-bordered contactUsTextfields required email">
</div>
</div>
</div>
</div>
<div class="field-item animated" data-animate="fadeInUp" data-delay="1.0">
<label class="field-label textSubTittleColors ttu">Your Message</label>
<div class="field-wrap ">
<textarea name="contact-message" placeholder="Leave your question or comment here" class="input-bordered contactUsTextfields input-textarea required"></textarea>
</div>
</div>
<input type="text" class="d-none" name="form-anti-honeypot" value="">
<div class="row">
<div class="col-sm-5 text-right animated" data-animate="fadeInUp" data-delay="1.1">
<button type="submit" name="submit" class="btn sendButton" >SEND</button>
</div>
<div class="col-sm-7 order-sm-first">
<div class="form-results"></div>
</div>
</div>
</form>

How to send message to contact us page using PHP

I have a contact us page in html including a form where a user can send message with that form. I want to send the message with PHP mailer function. But the problem is after i sending the message I want to redirect to the page with message. But instead of loading the it's showing the response in a new page. How do i send the user to the same page again?
Here is my Contact.html
<?php
if (isset($arrResult)) {
if($arrResult['response'] == 'success') {
?>
<div class="alert alert-success" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.
</div>
<?php
} else if($arrResult['response'] == 'error') {
?>
<div class="alert alert-danger" id="contactError">
<strong>Error!</strong> There was an error sending your message. (<?php echo $arrResult['error'];?>)
</div>
<?php
}
}
?>
<h2 class="mb-sm mt-sm"><strong>Contact</strong> Us</h2>
<form id="contactForm" action="php/contact-form.php" method="POST">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name" required>
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message" id="message" required></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" onclick="myFunction()"class="btn btn-primary btn-lg mb-xlg" data-loading-text="Loading...">
</div>
</div>
</form>
</div>
Here is the contact-form.php
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php');
$email = 'myemail#yahoo.co';
$subject = $_POST['subject'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = '';
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug;
$mail->AddAddress($email);
$mail->SetFrom($email, $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
$arrResult = array ('response'=>'success');
} catch (phpmailerException $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}
if ($debug == 0) {
echo json_encode($arrResult);
}
Use header
header('Location: http://www.example.com/')
Header doc

PHP Script is correct, but not sending

I am having difficulty with my php code. From my tired eyes, the code is correct, and I've had multiple others look at the code. No one can figure out why it's not working. It must be something quite simple, but I cannot get the contact form to send.
PHP script:
<?php
$from = 'email#example.com';
$sendTo = 'email#example.com';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$htmlHeader = '';
$htmlFooter = '';
$okMessage = 'Contact form succesfully submitted. Thank you, We will get back to you soon!';
$htmlContent = '<h1>New message from contact form</h1>';
use Nette\Mail\Message,
Nette\Mail\SendmailMailer;
require 'php/Nette/nette.phar';
$configurator = new Nette\Configurator;
$configurator->setTempDirectory(__DIR__ . '/php/temp');
$container = $configurator->createContainer();
$httpRequest = $container->getService('httpRequest');
$httpResponse = $container->getService('httpResponse');
$post = $httpRequest->getPost();
if ($httpRequest->isAjax()) {
$htmlContent .= '<table>';
foreach ($post as $key => $value) {
if (isset($fields[$key])) {
$htmlContent .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$htmlContent .= '</table>';
$htmlBody = $htmlHeader . $htmlContent . $htmlFooter;
$mail = new Message;
$mail->setFrom($from)
->addTo($sendTo)
->setSubject($subject)
->setHtmlBody($htmlBody, FALSE);
$mailer = new SendmailMailer;
$mailer->send($mail);
$responseArray = array('type' => 'success', 'message' => $okMessage);
$httpResponse->setCode(200);
$response = new \Nette\Application\Responses\JsonResponse($responseArray);
$response->send($httpRequest, $httpResponse);
}
Contact Form HTML:
<div class="section contact soepa" id="contact" data-animate="bounceIn">
<div class="container">
<div class="col-md-12">
<h2 class="title"><span style="color: #f46b01;">Connect With Us</span></h2>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form id="contact-form" method="post" action="contact.php">
<div class="messages">
</div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" class="form-control" placeholder="Your firstname *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="surname" class="form-control" placeholder="Your lastname *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="email" class="form-control" placeholder="Your email *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="phone" class="form-control" placeholder="Your phone *" required="required">
</div>
<div class="col-md-12">
<textarea name="message" class="form-control" placeholder="Message *" rows="4" required="required"></textarea>
</div>
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary btn-lg" value="Send message">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

How do I enable SMTP for my PHP contact form?

I have successfully set up a php script for my contact form on my website but I have recently found out that my server provider does not accept php. Instead I have use SMTP.
Can anyone help me as this is new to me. I've attempted using other scripts but I cannot implement it.
This is my php code:
<?php
/* Set e-mail recipient */
$myemail = "mail#louisreed.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
My HTML form:
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
Any suggestions welcome!
Thanks! :)
Louis
Oke I checked a few thinks. I guess you could atleast try the following. I know your host doesn't support php which find hard to believe tough. So maybe something else is wrong. What is the host you are using?
<?php
if($_POST) {
//Strip user input
function sanitize($value) {
//Escape the user input and then strip all html tags
//mysql_* is not recommended but I guess you don't store data in a database
$value = mysql_real_escape_string(strip_tags($value));
//Return the sanitized user input
return $value;
}
//To display all errors
function errors($error) {
echo '<ul class="error">';
foreach($error as $fail) {
echo '<li>'.$fail.'</li>';
}
echo '</ul>';
}
//All the input fields
$name = sanitize($_POST['name']);
$email = sanitize($_POST['email']);
$subject = sanitize($_POST['subject']);
$message = sanitize($_POST['message']);
//Check if there are no empty fields
if(!empty($name), !empty($email) && !empty($subject) && !empty($message)) {
if(strlen($name) <= 3) {
$error[] = 'Name '.$name.' is too short';
}
if(strlen($email) <= 3) {
$error[] = 'Email '.$email.' is too short';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Email '.$email.' is not a valid email';
}
if(strlen($subject) <= 3) {
$error[] = 'Subject '.$subject.' is too short';
}
if(strlen($message) <= 8) {
$error[] = 'Message is too short';
}
//If there are no errors
if(empty($error)) {
$to = "mail#louisreed.co.uk"; //The email you want to send it to
//Subject already set
$headers = "FROM: My site"; //Sets the headers
//The message for the email
$message = "Someone has sent you a message using your contact form:\r\n\r\nName: ".$name."\r\nEmail: ".$email."\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
$mail = mail($to, $subject, $message, $headers);
//If the mail has been send
if($mail) {
header('Location: http://louisreed.co.uk/index.php?success');
exit();
}
}
} else {
$error[] = 'There are empty fields';
}
//If there are errors show them
if(!empty($error)) {
echo errors($error);
}
}
//If issset success (from the header()) display a message
if(isset($_GET['success'])) {
echo '<p>Thank you for your message</p>';
}
?>
<form role="form" action="" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
So try the exact code I gave you above just copy and past it over the form code you have now. You can leave the rest of your code as is. After that save the complete file as index.php. After that remove the index.html file from your FTP. If you have done that you can upload the index.php file now check what you get. If there are still some errors please let me know

Categories