So I’ve updated the code below but it still doesn’t work and I don’t understand why… The messages doesn’t appear in my database in PhpMyAdmin and I don’t see the error messages I’ve put when for example a entry in my form is empty.. Thank you in advance for helping me…………………………………………………………………………………………………………………………………………………………………..
<?php $pdo = new PDO( 'mysql:host=localhost;dbname=portfolio', 'root', '', array( PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ) ); $message = ''; if (!empty($_POST)) { //Contrôle des données : if (!isset($_POST['nom']) || strlen($_POST['nom']) < 3 || strlen($_POST['nom']) > 50) { $message .= '<div>Le champ "nom" doit comporter entre 3 et 20 caractères.</div>'; } if (!isset($_POST['prenom']) || strlen($_POST['prenom']) < 3 || strlen($_POST['prenom']) > 50) { $message .= '<div>Le champ "prénom" doit comporter entre 2 et 20 caractères.</div>'; } if (!isset($_POST['email']) || strlen($_POST['email'] > 50) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $message .= '<div class="alert alert-danger">L'email n'est pas valide</div>'; } if(!isset($_POST) || !preg_match('#^[0-9]{10}$#', $_POST['telephone']) ) { $message .= '<div class="alert alert-danger">Le champ "téléphone" doit comporter 10 chiffres</div>'; } if (!isset($_POST['sujet']) || strlen($_POST['sujet']) < 3 || strlen($_POST['sujet']) > 100) { $message .= '<div>Le champ "sujet" doit comporter entre 2 et 20 caractères.</div>'; } if(!isset($_POST['message']) || strlen($_POST['message']) < 3 || strlen($_POST['message']) > 500 ) { $message .= '<div class="alert alert-danger">Le message est trop long !</div>'; } if(empty($message)) { foreach ($_POST as $indice => $valeur) { $_POST[$indice] = htmlspecialchars($valeur, ENT_QUOTES); } $resultat = $pdo->prepare("INSERT INTO message(nom, prenom, email, telephone, sujet, message) VALUES(:nom, :prenom, :email, :telephone, :sujet, :message)"); $succes = $resultat->execute(array( ':nom' => $_POST['nom'], ':prenom' => $_POST['prenom'], ':email' => $_POST['email'], ':telephone' => $_POST['telephone'], ':sujet' => $_POST['sujet'], ':message' => $_POST['message'] )); if ($succes) { $message .= '<div>Le message a bien été envoyé !</div>'; } else { $message .= '<div>Une erreur est survenue...</div>'; } }// if (empty($message)) }//if (!empty($_POST)) <form action="" method="post" class="container form-container"> <div class="row justify-content-center"> <input id="nom" name="nom" class="form-control" type="text" value="<?= $_POST['nom'] ?? '';?>" placeholder="Nom"> <input id="prenom" name="prenom" class="form-control" type="text" value="<?= $_POST['prenom'] ?? '';?>" placeholder="Prénom"> <input id="email" name= "email" class="form-control" type="text" value="<?= $_POST['email'] ?? '';?>"placeholder="Email"> <input id="telephone" name="telephone" class="form-control" type="text" value="<?= $_POST['telephone'] ?? '';?>"placeholder="Téléphone"> <input id="sujet" name="sujet" class="form-control" type="text" value="<?= $_POST['sujet'] ?? '';?>"placeholder="Sujet"> <textarea id="message" name="message" class="form-control" cols="30" rows="10" value=""placeholder="Votre message"><?= $_POST['message'] ?? '';?></textarea> <input class="btn btn-primary" type="submit" value="Envoyer"> </div> </form>
Advertisement
Answer
The name attributes are missing in your form – so nothing gests postet.
It should be:
**UPDATED 4.1.2020 19:03 CET textarea was wrong, sujet vs subject! **
<form action="" method="post" class="container form-container"> <div class="row justify-content-center"> <input id="nom" name="nom" class="form-control" type="text" value="<?= $_POST['nom'] ?? '';?>" placeholder="Nom"> <input id="prenom" name="prenom" class="form-control" type="text" value="<?= $_POST['prenom'] ?? '';?>" placeholder="Prénom"> <input id="email" name= "email" class="form-control" type="text" value="<?= $_POST['email'] ?? '';?>"placeholder="Email"> <input id="telephone" name="telephone" class="form-control" type="text" value="<?= $_POST['telephone'] ?? '';?>"placeholder="Téléphone"> <input id="sujet" name="sujet" class="form-control" type="text" value="<?= $_POST['sujet'] ?? '';?>"placeholder="Sujet"> <textarea id="message" name="message" class="form-control" cols="30" rows="10" value=""placeholder="Votre message"><?= $_POST['message'] ?? '';?></textarea> <input class="btn btn-primary" type="submit" value="Envoyer"> </div>
Additionally this is wrong
setting a variable first then asking for its content is not very useful 😉 additionally this variable IS NOT SET ANYWHERE ELSE?!?
$succes = ''; // delete this line and set the variable above in the script if ($succes) { $message .= '<div>Le message a bien été envoyé !</div>'; } else { $message .= '<div>Une erreur est survenue...</div>'; }
so it should be you have to assign a value to succes – for example if you want to check the insert:
$succes= $resultat->execute(array( ':nom' => $_POST['nom'], ':prenom' => $_POST['prenom'], ':email' => $_POST['email'], ':telephone' => $_POST['telephone'], ':sujet' => $_POST['sujet'], ':message' => $_POST['message'] )); //$succes = ''; // NO! //do not just **write $var='';** when you get a variable is undefined error if ($succes) { $message .= '<div>Le message a bien été envoyé !</div>'; } else { $message .= '<div>Une erreur est survenue...</div>'; }
I also think that the last if condition is wrong and should be like this:
It cannot be EMPTY AND larger than 500 chars 😉 and the [‘message’] was missing in the first condition
if(empty($_POST['message']) || strlen($_POST['message']) < 3 || strlen($_POST['message']) > 500 ) { $message .= '<div class="alert alert-danger">Le message est trop long !</div>'; }
############################################################################### FINAL UPATE: here is the complete fixed code with some debug additions
<?php $pdo = new PDO( 'mysql:host=localhost;dbname=portfolio', 'root', '', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ) ); $message = ''; if (!empty($_POST)) { //Contrôle des données : if (!isset($_POST['nom']) || strlen($_POST['nom']) < 3 || strlen($_POST['nom']) > 50) { $message .= '<div>Le champ "nom" doit comporter entre 3 et 20 caractères.</div>'; } if (!isset($_POST['prenom']) || strlen($_POST['prenom']) < 3 || strlen($_POST['prenom']) > 50) { $message .= '<div>Le champ "prénom" doit comporter entre 2 et 20 caractères.</div>'; } if (!isset($_POST['email']) || strlen($_POST['email'] > 50) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $message .= '<div class="alert alert-danger">L'email n'est pas valide</div>'; } if(!isset($_POST) || !preg_match('#^[0-9]{10}$#', $_POST['telephone']) ) { $message .= '<div class="alert alert-danger">Le champ "téléphone" doit comporter 10 chiffres</div>'; } if (!isset($_POST['sujet']) || strlen($_POST['sujet']) < 3 || strlen($_POST['sujet']) > 100) { $message .= '<div>Le champ "sujet" doit comporter entre 2 et 20 caractères.</div>'; } if(!isset($_POST['message']) || strlen($_POST['message']) < 3 || strlen($_POST['message']) > 500 ) { $message .= '<div class="alert alert-danger">Le message est trop long !</div>'; } if(empty($message)) { //echo"<br>message empty<br>"; foreach ($_POST as $indice => $valeur) { $_POST[$indice] = htmlspecialchars($valeur, ENT_QUOTES); } $resultat = $pdo->prepare("INSERT INTO messages(nom, prenom, email, telephone, sujet, message) VALUES(:nom, :prenom, :email, :telephone, :sujet, :message)"); if (!$resultat) { $message .= var_dump($resultat->errorInfo());} else{ $message .= "PDO prepare sucessful";} //var_dump($_POST); $succes = $resultat->execute(array( ':nom' => $_POST['nom'], ':prenom' => $_POST['prenom'], ':email' => $_POST['email'], ':telephone' => $_POST['telephone'], ':sujet' => $_POST['sujet'], ':message' => $_POST['message'] )); //changed for debug if ($succes) { $message .='<div>Le message a bien été envoyé !</div>'; $message .= "PDO execute sucessful<br>";} else { $message .= '<div>Une erreur est survenue...</div>'; $message .= var_dump($resultat->errorInfo()); //for debug only } echo "MESSAGE saved!<br>". $message; } else { echo "ERROR: ". $message; } } if (empty($_POST)) { ?> <form action="" method="post" class="container form-container"> <div class="row justify-content-center"> <input id="nom" name="nom" class="form-control" type="text" value="<?php echo $_POST['nom'] ?? '';?>" placeholder="Nom"> <input id="prenom" name="prenom" class="form-control" type="text" value="<?php echo $_POST['prenom'] ?? '';?>" placeholder="Prénom"> <input id="email" name="email" class="form-control" type="text" value="<?php echo $_POST['email'] ?? '';?>"placeholder="Email"> <input id="telephone" name="telephone" class="form-control" type="text" value="<?php echo $_POST['telephone'] ?? '';?>"placeholder="Téléphone"> <input id="sujet" name="sujet" class="form-control" type="text" value="<?php echo $_POST['sujet'] ?? '';?>" placeholder="Sujet"> <textarea id="message" name="message" class="form-control" cols="30" rows="10" value=""placeholder="Votre message"><?php echo $_POST['message'] ?? '';?></textarea> <input class="btn btn-primary" type="submit" value="Envoyer"> </div> </form> <?php } ?>