Success Message not visible on my web host [duplicate] - php

This question already has an answer here:
"Cannot send session cache limiter - headers already sent" [duplicate]
(1 answer)
Closed 4 years ago.
I create a website with contact page in it. My probleme is when i click on send, it sends the message but it doesn't display my success division which contains the success text: "your message has been sent successfully".
I can see this division with a local host but not in my web host.
Here is my code: contact.php
<?php
session_start();
?>
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Nous Contacter</h2>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<?php if(array_key_exists('errors',$_SESSION)): ?>
<div class="alert alert-danger">
<?= implode('<br>', $_SESSION['errors']); ?>
</div>
<?php endif; ?>
<?php if(array_key_exists('success',$_SESSION)): ?>
<div class="alert alert-success">
Votre email à bien été transmis !
</div>
<?php endif; ?>
<form action="php/contact-us.php" method="post" id="contactForm" role="form">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Votre Nom *" id="name" name="name" value="<?php echo isset($_SESSION['inputs']['name'])? $_SESSION['inputs']['name'] : ''; ?>">
<p class="comments text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Votre Email *" id="email" name="email" value="<?php echo isset($_SESSION['inputs']['email'])? $_SESSION['inputs']['email'] : ''; ?>">
<p class="comments text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Téléphone *" id="phone" name="phone" pattern="[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}" value="<?php echo isset($_SESSION['inputs']['phone'])? $_SESSION['inputs']['phone'] : ''; ?>">
<p class="comments text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Message *" id="message" name="message" <?php echo isset($_SESSION['inputs']['message'])? $_SESSION['inputs']['message'] : ''; ?>></textarea>
<p class="comments text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<input type="submit" class="btn btn-default abt-btn" value="Envoyer"/>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
contact-us.php
<?php
session_start();//on démarre la session
// $errors = [];
$errors = array(); // on crée une vérif de champs
if(!array_key_exists('name', $_POST) || $_POST['name'] == '') {// on verifie l'existence du champ et d'un contenu
$errors ['name'] = "vous n'avez pas renseigné votre nom";
}
if(!array_key_exists('phone', $_POST) || $_POST['phone'] == '') {// on verifie l'existence du champ et d'un contenu
$errors ['name'] = "vous n'avez pas renseigné votre N° de Téléphone";
}
if(!array_key_exists('email', $_POST) || $_POST['email'] == '' || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {// on verifie existence de la clé
$errors ['mail'] = "vous n'avez pas renseigné votre email";
}
if(!array_key_exists('message', $_POST) || $_POST['message'] == '') {
$errors ['message'] = "vous n'avez pas renseigné votre message";
}
/*if(array_key_exists('antispam', $_POST)) {// on place un petit filet anti robots spammers
$errors ['antispam'] = "Vous êtes un robots spammer";
}*/
//On check les infos transmises lors de la validation
if(!empty($errors)){ // si erreur on renvoie vers la page précédente
$_SESSION['errors'] = $errors;//on stocke les erreurs
$_SESSION['inputs'] = $_POST;
header('Location: contact.php');
}else{
$_SESSION['success'] = 1;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'FROM:' . htmlspecialchars($_POST['email']);
$to = 'contact#gmail.com'; // Insérer votre adresse email ICI
$subject = 'Message envoyé par ' . htmlspecialchars($_POST['name']) .' - Téléphone: ' . htmlspecialchars($_POST['phone']) .' - <i>' . htmlspecialchars($_POST['email']) .'</i>';
$message_content = '
<table>
<tr>
<td><b>Emetteur du message:</b></td>
</tr>
<tr>
<td>'. $subject . '</td>
</tr>
<tr>
<td><b>Contenu du message:</b></td>
</tr>
<tr>
<td>'. htmlspecialchars($_POST['message']) .'</td>
</tr>
</table>
';
mail($to, $subject, $message_content, $headers);
header('Location: contact.php');
}
?>
Edit
I found the solution in this topic:
"Cannot send session cache limiter - headers already sent"

You have not called session_start(). Without calling session_start(), the $_SESSION super global variable will not be populated with the session data. See: http://php.net/manual/en/function.session-start.php
You need to call session_start() in contact.php. For example add the following to the top of contact.php:
<?php
session_start();
?>

Related

Problem in transmitting data from an HTML / PHP form

Good evening to all,
I did a project with Argon (Bootstrap / vue.js). This is a SPA with a contact section including a contact form (name, email, message). My contact form, is established on a .vue file, who is imported into my index.html:
<form class="needs-validation col-md-8 from-right" novalidate id="myForm" method="post" action="./index.php">
<div class="form-column">
<div>
<label for="nom">Saisissez votre nom</label>
<input type="text" class="form-control" id="nom" required placeholder="Votre nom" name="nom">
<div class="valid-feedback">
C'est noté!
</div>
<div class="invalid-feedback">
Merci de saisir votre nom.
</div>
</div>
<div>
<label for="email" class="mt-3">Saisissez votre email</label>
<input type="email" class="form-control" id="email" placeholder="Votre email" name="email" required>
<div class="valid-feedback">
C'est noté!
</div>
<div class="invalid-feedback">
Merci de saisir un email valide.
</div>
</div>
<div>
<label for="message" class="mt-3">Saisissez votre message</label>
<textarea class="form-control" id="message" placeholder="Votre message ici" name="message" required></textarea>
<div class="valid-feedback">
C'est noté!
</div>
<div class="invalid-feedback">
Merci de saisir votre message.
</div>
</div>
</div>
<button class="btn btn-primary my-5 mx-auto" type="submit">Envoyer</button>
<div id="retourenvoimessage"></div>
</form>
I established an index.php file, supposed to retrieve the data from the HTML form, and forward it to me by email. I apologize in advance if my code offends PHP enthusiasts, for my part I do not know this language at all for the moment and have tried to get by by drawing on some documentation:
<?php
$nom = $_POST['nom'];
$email = $_POST['email'];
$message = $_POST['message'];
$dest = "fer.mathieu#gmail.com";
$sujet = "Demande de contact";
$corp = "Nom : $nom\n Email : $email\n Message : $message";
$headers = "From: $nom\n Reply-To: $email";
if (mail($dest, $sujet, $corp, $headers)) {
?><script>document.querySelector('#retourenvoimessage').textContent="Message bien envoyé, merci!"</script><?php
} else {
?><script>alert("Oups, un problème est survenu lors de l'envoi du message, désolé...");document.location.href="http://www.namesite.com/"</script><?php
}
?>
My wish was that during the submission, if successful, a message is added specifying the sending.
I have a javascript function which prevents the default behavior and therefore does not reload the page when submitting.
Yesterday I put my project online, and there "is the drama". When I fill in the fields of the form and submit it, firstly no message is added as wanted, but well that's not what bothers me the most.
I did receive an email from my host, containing the requested fields ('Name:', 'Email:', 'Message: "), but these are empty.
After much research on the net, not being able to find an answer to my problem, I motivate myself to post this one, at the risk of being hit with sticks if the problem, my mistake, was very simple Smiley confused.
Thank you in advance and sorry for my bad english.
The problem is probably caused by my Vue file. I do a new php file with PhpMailer, who works well in local mode. But when i submit my form, i no longer receive email as expected.
My Vue.js file:
<form class="needs-validation col-md-8 from-right" novalidate id="myForm" method="post" action="index.php">
<div class="form-column">
<div>
<label for="nom">Saisissez votre nom</label>
<input v-model="nom" type="text" class="form-control" id="nom" required placeholder="Votre nom" name="nom">
<div class="valid-feedback">
C'est noté!
</div>
<div class="invalid-feedback">
Merci de saisir votre nom.
</div>
</div>
<div>
<label for="email" class="mt-3">Saisissez votre email</label>
<input v-model="email" type="email" class="form-control" id="email" placeholder="Votre email" name="email" required>
<div class="valid-feedback">
C'est noté!
</div>
<div class="invalid-feedback">
Merci de saisir un email valide.
</div>
</div>
<div>
<label for="message" class="mt-3">Saisissez votre message</label>
<textarea v-model="message" class="form-control" id="message" placeholder="Votre message ici" name="message" required></textarea>
<div class="valid-feedback">
C'est noté!
</div>
<div class="invalid-feedback">
Merci de saisir votre message.
</div>
</div>
</div>
<button class="btn btn-primary my-5 mx-auto" type="submit">Envoyer</button>
</form>
</div>
</section>
</template>
<script>
import BaseNav from "#/components/BaseNav";
import CloseButton from "#/components/CloseButton";
export default {
name: 'Contact',
data(){
return {
nom: '',
email: '',
message: ''
}
}
};
</script>
My PHP file:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/PHPMailer/src/Exception.php';
require 'lib/PHPMailer/src/PHPMailer.php';
require 'lib/PHPMailer/src/SMTP.php';
$nom = $_POST['nom'];
$email = $_POST['email'];
$message = $_POST['message'];
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myadress#gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom($email, $nom);
$mail->addAddress('myadress#gmail.com');
// Content
$mail->isHTML(true);
$mail->Subject = 'Contact via portfolio';
$mail->Body = "Nom : $nom\n
Email : $email\n
Message : $message";
$mail->send();
echo 'Message bien envoyé';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
The Javascript function for the form:
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();

Contact form (Bootstrap, html & php)

I just made my contact form to function, sort of. It has a few issues:
When the form is sent, it kicks you off the page, directs you to a blank page with the message: "Thank you etc". I would love to stay on the page, and just either get a box pop-up with the success message or just have it on the page. Either is fine :)
The error message doesn't show, it just puts you on a blank page.
The errMessages like "please fill in a valid e-mail" don't show. Probably because I removed the labels, but I would like to show that message IN the box rather than under it.
It's overriding my CSS! But that happens sometimes on my computer, one time I see lines other times I see boxes. So that issue might be somewhere else. However with the 'working form' it changes my last box.
This is what I meant with changing CSS on my boxes
PHP code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'denise#hetfulfilmentbedrijf.nl';
$subject = 'Message from Contact Demo';
$body = "From: $name\n Subject: $subject\n Number: $phone\n E-Mail: $email\n Message:\n $message";
//Check if name has been entered
if(!$_POST['name']) {
$errName = 'Vul alsjeblieft je naam in';
}
//Check if subject has been entered
if(!$_POST['subject']) {
$errName = 'Vul alsjeblieft een onderwerp in';
}
//Check if number has been entered
if(!$_POST['phone']) {
$errName = 'Vul alsjeblieft je nummer in';
}
//Check if e-mail is entered and valid
if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vul alsjeblieft je e-mailadres in';
}
//Check if message has been entered
if(!$_POST['email']) {
$errMessage = 'Laat alsjeblieft een bericht achter';
}
// If no errors, send email
if (!$errName && !$errSubject && !$errPhone && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
echo $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
}
else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
HTML:
<form class="form-horizontal" role="form" method="post" action="partials/contactform.php">
<div class="form-group offset-top-45">
<textarea rows="11" cols="100" class="form-control" id="message" name="message" placeholder="Laat hier je bericht voor ons achter:"></textarea><?php echo htmlspecialchars($_POST['message']);?></textarea>
<span class="help-block" style="display: none;">Laat alsjeblieft een bericht achter</span>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="col-md-6">
<div class="row offset-top-10">
<div class="pull-right">
<img height="60" width="100" src="/images/stamp.png" alt="stamp">
</div>
</div>
<div class="row offset-top-10" style="padding-right:20px; padding-left:10px">
<form role="form" id="feedbackForm">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Hoe heet je?" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft je naam in</span>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Wat is het onderwerp?" value="<?php echo htmlspecialchars($_POST['subject']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft een onderwerpin</span>
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Op welk nummer kunnen wij jou bereiken?" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft je nummer in</span>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Wat is je e-mailadres?">
<span class="help-block" style="display: none;">Vul alsjeblieft je e-mail in</span>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<div class="col-md-12 text-right">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-s">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
</form>
Help is much appreciated!
Full script it should work without any problems
<?php
$result = "";
$errors = ""; //use to count errors
//Error message variables
$errName = "";
$errSubject = "";
$errEmail = "";
$errMessage = "";
//message variables
$name = "";
$subject = "";
$phone = "";
$email = "";
$message = "";
$to = "denise#hetfulfilmentbedrijf.nl";
if (isset($_POST['submit'])) {
//check if name has been entered
if (empty($_POST['name'])) {
$errName = "Vul alsjeblieft je naam in";
$errors++;
} else {
$name = UserInput($_POST['name']);
}
////Check if subject has been entered
if (empty($_POST['subject'])) {
$errSubject = "Vul alsjeblieft een onderwerp in";
$errors++;
} else {
$Subject = UserInput($_POST['subject']);
}
//check if email entered
if (empty($_POST['email'])) {
$errEmail = "Laat alsjeblieft een bericht achter";
$errors++;
} else {
$email = UserInput($_POST['email']);
// check if email is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
$errEmail = "Vul alsjeblieft je e-mailadres in";
$errors++;
}
}
if (empty($_POST['phone'])) {
$errPhone = "Vul alsjeblieft je nummer in";
$errors++;
} else {
$phone = UserInput($_POST['phone']);
// check if email is numbers
if (!is_numeric($phone)) {
$errPhone = "enter numbers only";
$errors++;
}
}
//check message
if (empty($_POST['message'])) {
$errMessage = "Laat alsjeblieft een bericht achter";
} else {
$message = UserInput($_POST['message']);
}
if ($errors > 0) {
// we have errors do not send email
$result = "<div class=\"alert alert-danger\">Sorry there was an error sending your message. Please fix " . $errors . " errors on the form </div>";
} else {
//no errors set email headers and send email
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <' . $email . '>' . "\r\n";
$body = "<p> New email from $name";
$body .= "<p> Phone : $phone</p>";
$body .= "<p> Email : $email<p>";
$body .= "<p>Message : $message</p>";
if (mail($email, $subject, $msg, $header)) {
$result = "<div class=\"alert alert-success\">Thank You! I will be in touch</div>";
$_POST = array(); //clear the form aftter sendig
} else {
$result = "<div class=\"alert alert-danger\">Sorry there was an error sending your message. Please try again later</div>";
}
}
}
//sanitise use input
function UserInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form class="form-horizontal" role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<div class="col-md-12 text-right">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-s">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
<div class="form-group offset-top-45">
<textarea rows="11" cols="100" class="form-control" id="message" name="message" placeholder="Laat hier je bericht voor ons achter:"></textarea><?php if(!empty($_POST['message'])){echo $_POST['message'];}?></textarea>
<?php echo "<p class=\"text-danger\">".$errMessage."</p>";?>
</div>
</div>
<div class="col-md-6">
<div class="row offset-top-10">
<div class="pull-right">
<img height="60" width="100" src="/images/stamp.png" alt="stamp">
</div>
</div>
<div class="row offset-top-10" style="padding-right:20px; padding-left:10px">
<form role="form" id="feedbackForm">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Hoe heet je?" <?php if(!empty($_POST['name'])){echo "value=\"".$_POST['name']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errName."</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Wat is het onderwerp?" <?php if(!empty($_POST['Subject'])){echo "value=\"".$_POST['Subject']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errSubject."</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Op welk nummer kunnen wij jou bereiken?" <?php if(!empty($_POST['phone'])){echo "value=\"".$_POST['phone']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errPhone."</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Wat is je e-mailadres?">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</form>
Let me know if you have any question or need any help.

Contact form with select field issues

I've got some troubles about my contact form, i tried many solutions but it still doesnt work.
I've got a basic contact form with basic inputs and a select field. All the fields are sent by email except the datas in my select field.
Here's my HTML form :
<section class="section-signup bg-faded mt80">
<div class="container">
<div class="row">
<h3 class="text-xs-center">Je veux devenir un point de vente</h3>
<p class="subform m-b-3">Merci de préciser si vous êtes une entreprise, un courtier, un investisseur, un professionnel ou autre</p>
<div class="form-bottom contact-form">
<form role="form" action="contact.php" method="post">
<div class="form-group col-md-4">
<label for="email_expediteur">Votre adresse e-mail :</label>
<input type="text" name="email" placeholder="Email..." class="contact-email form-control" id="contact-email">
</div>
<div class="form-group col-md-4">
<label for="email_expediteur">Sujet :</label>
<input type="text" name="subject" placeholder="Sujet..." class="contact-subject form-control" id="contact-subject">
</div>
<div class="col-md-4 form-group">
<label for="jesuis">Je suis : </label>
<select class="form-control" name="jesuis">
<option value="">Selectionnez...</option>
<option value="entreprise">Une entreprise</option>
<option value="courtier">Un courtier</option>
<option value="investisseur">Un investisseur</option>
<option value="professionnel">Un professionnel</option>
<option value="autre">Autre</option>
</select>
</div>
<div class="col-md-12 form-group">
<label for="contact-message">Message :</label>
<textarea name="message" placeholder="Message..." class="contact-message form-control" id="contact-message"></textarea>
</div>
<div class="col-md-12 form-group">
<label for="contact-antispam">Question Antispam : 7 + 5 = ?</label>
<input type="text" name="antispam" placeholder="Votre réponse..." class="contact-antispam form-control" id="contact-antispam">
</div>
<div class="col-md-6 col-md-offset-3 form-group centered">
<button type="submit" class="btn">Envoyer</button>
</div>
</form>
</div>
</div>
</div>
</section>
And here's my contact.php :
<?php
// Email address verification
function isEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'info#yachtbox.net';
$clientEmail = addslashes(trim($_POST['email']));
$subject = addslashes(trim($_POST['subject']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$jesuis = addslashes(trim($_POST['jesuis']));
$array = array('emailMessage' => '', 'subjectMessage' => '', 'messageMessage' => '', 'antispamMessage' => '', 'jesuisMessage' =>'');
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Email Invalide';
}
if($subject == '') {
$array['subjectMessage'] = 'Sujet manquant';
}
if($message == '') {
$array['messageMessage'] = 'Message manquant';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Mauvaise réponse';
}
if($jesuis == '') {
$array['jesuisMessage'] = 'Merci de préciser si vous êtes une entreprise, un courtier, un investisseur, un professionnel ou autre';
}
if(isEmail($clientEmail) && $subject != '' && $message != '' && $antispam == '12' && $jesuis != '') {
// Send email
$headers = "From: " . $clientEmail . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject . " (Formulaire de contact MUST Assurances)", $message, $headers, $jesuis);
}
echo json_encode($array);
}
?>
Do you have any ideas ?
Cheers.
Loïc
$message = "Je suis un(e) ".$jesuis." ".$message;
mail($emailTo, $subject . " (Formulaire de contact MUST Assurances)", $message, $headers);
Your 5th parameter didn't make sense.

PHP in bootstrap form not working

I have been trying to add php to a bootstrap form via the following tutorial: https://bootstrapbay.com/blog/working-bootstrap-contact-form/. But nothing happens when I click the submit button - I just go back/stay to the same page with an empty form. I don't receive an email nor any error actions when I don't fill out some fields either. Below my code. The name of the document is werkwijze.php. Does someone have an idea what I'm doing wrong? Thanks a lot in advance!
<?php
if (isset($_POST['submit'])){
$voornaam = $_POST['voornaam'];
$familienaam = $_POST['familienaam'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$vraag = $_POST['vraag'];
$from = 'info#tbep.be';
$to = 'eefje.vanhemelryck#gmail.com';
$subject = 'Vraag van op uw website www.tbep.be';
$body = "From: $voornaam $familienaam\n E-Mail: $email\n Telefoon: $tel\n Vraag: $vraag";
// Check if name has been entered
if (!$_POST['voornaam']) {
$errVoornaam = 'Gelieve uw voornaam op te geven';
}
if (!$_POST['familienaam']) {
$errFamilienaam = 'Gelieve uw familienaam op te geven';
}
// Check if email en phone has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Gelieve uw emailadres op te geven';
}
if (!$_POST['tel']) {
$errTel = 'Gelieve uw telefoonnummer op te geven';
}
//Check if message has been entered
if (!$_POST['vraag']) {
$errVraag = 'Gelieve uw vraag te stellen';
}
// If there are no errors, send the email
if (!$errVoornaam && !$errFamilienaam && !$errEmail && !$errTel && !$errVraag) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Hartelijk dank. Ik neem zo snel mogelijk contact met u op!</div>';
} else {
$result='<div class="alert alert-danger">Sorry, er was een probleem met het versturen van dit formulier. Alternatief kan u ons een email sturen op info#tbep.be</div>';
}
}
}
?>
<form class="form-horizontal" role="form" action="werkwijze.php" method="post" enctype="text/plain">
<div class="form-group">
<label class="control-label col-sm-3" for="voornaam">Voornaam:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="voornaam" name="voornaam" placeholder="Voornaam">
<?php echo "<p class='text-danger'>$errVoornaam</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="familienaam">Familienaam:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="familienaam" name="familienaam" placeholder="Familienaam">
<?php echo "<p class='text-danger'>$errFamilienaam</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email" name="email" placeholder="Email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="telefoon">Telefoonnummer:</label>
<div class="col-sm-9">
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefoonnummer">
<?php echo "<p class='text-danger'>$errTel</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="vraag">Uw vraag:</label>
<div class="col-sm-9">
<textarea class="form-control" rows="5" id="vraag" name="vraag" placeholder="Uw vraag"></textarea>
<?php echo "<p class='text-danger'>$errVraag</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" id="submit" name="submit" value="Send" class="btn btn-default">Versturen</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
<?php echo $result; ?>
</div>
</div>
</form>
Please Remove enctype="text/plain" from your form.
<form class="form-horizontal" role="form" action="werkwijze.php" method="post">
Or use
<form class="form-horizontal" role="form" action="werkwijze.php" method="post" enctype="multipart/form-data">

Ussing reCAPTCHA on HTML5 whit contact-form.php

I have a website Html5 and try adding the reCaptcha still sending messages without activating the reCaptcha and last night I received over 300 messages a boot.
Help me please how to add so that only sent when the button is activated reCaptcha.
Send sends works well but not activation reCaptcha.
To start contact.html within my template I have put this way:
<!-- Start formulario de contacto -->
<div class="row">
<div class="col-md-9">
<h2>Formulario de contacto</h2>
<form action="php/contact-form.php" id="contact-form">
<div class="alert alert-success hidden" id="contact-alert-success">
<strong>Mensaje enviado correctamente!</strong> Muchas gracias, pronto nos pondremos en contacto con usted, normalmente nuestro tiempo de respuesta es inferior a 2 horas.
</div>
<div class="alert alert-danger hidden" id="contact-alert-error">
<strong>Error!</strong> A sucedido un error si lo desea puede contactarnos directamente en XXXX#tize.XXXX
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Nombre <span class="required">*</span></label>
<input type="text"
value=""
data-msg-required="Por favor introduzca su nombre"
class="form-control"
name="name" id="name">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>eMail <span class="required">*</span></label>
<input type="email"
value=""
data-msg-required="Por favor introduzca su eMail"
data-msg-email="Por favor introduzca un eMail válido"
class="form-control"
name="email"
id="email">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Asunto <span class="required">*</span></label>
<input type="text"
value=""
data-msg-required="Por favor introduzca el asunto"
class="form-control"
name="subject"
id="subject">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Mensaje <span class="required">*</span></label>
<textarea
data-msg-required="Por favor introduzca su mensaje"
rows="10"
class="form-control"
name="message"
id="message"></textarea>
</div>
</div>
</div>
<!-- Start Google Recaptcha -->
<div class="g-recaptcha" data-sitekey="6Lc88P4SAAAAANiT-ZXILUo-ET4xQmbivHy7uHc8"></div><br>
<!-- End Google Recaptcha -->
<div class="row">
<div class="col-md-12">
<input type="submit" value="Enviar mensaje" class="btn btn-primary" data-loading-text="Cargando...">
</div>
</div>
</form>
</div>
<!-- End formulario de contacto -->
And in php form to send the messages have this post with contact-form.php :
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
// Enter your email address
$to = 'XXXX#tize.XX';
$subject = $_POST['subject'];
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$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";
}
$headers = '';
$headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if (mail($to, $subject, $message, $headers)){
$arrResult = array ('response'=>'success');
} else{
$arrResult = array ('response'=>'error');
}
echo json_encode($arrResult);
} else {
$arrResult = array ('response'=>'error');
echo json_encode($arrResult);
}
?>
Picture of my form, If anyone wants to see my website please let me know and send you the link. Thank you very much.
sending without activating the reCaptcha http://goo.gl/oSLQG9

Categories