Erreur(s) dans Formulaire de contact php ? [Résolu/Fermé]

Signaler
Messages postés
74
Date d'inscription
mercredi 18 avril 2012
Statut
Membre
Dernière intervention
15 octobre 2017
-
Messages postés
23144
Date d'inscription
samedi 22 octobre 2005
Statut
Modérateur
Dernière intervention
11 juin 2021
-
Bonjour ,

Après avoir suivie ce tuto :
  • h t t p s : / / ww w. youtube.com/watch?v=Dw9R0NEXuYo *


ma page " localhost " n'affiche rien ...
je soupçonne qu'une erreur(s) se soit glissé

index.php
 <?php
include '_inc.php';
?> <!DOCTYPE html>
<html>

<head lang="fr">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" herf="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"/>
<style>
body {
padding-top: 50px;
}
.starter-template {
padding-top: 40px;
}
</style>
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" herf="#">contact</a>
</div>
</div>
</nav>

<div class="contnaire">
<div class="starter-template">

<?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 a bien été envoyé
</div>
<?php endif; ?>


<form action="post_contact.php" method="POST">
<?php $form = new Form(isset($_SESSION['inputs']) ? $_SESSION['inputs'] : []); ?>
<div class="row">
<div class="col-xs-4"> <!-- " md " afficher les éléments à la vrtical , " -4 " pour 3 éléments , "-6" pour 2 éléments -->
<?= $form->text('name', 'Votre Nom'); ?>
</div>
<div class="col-xs-4">
<?= $form->email('email', 'Votre Email'); ?>
</div>
<div class="col-xs-4">
<?= $form->select('service', 'Votre Service', ['Contact', 'Dépanage', 'Heimerdigner']); ?>
</div>
<div class="col-xs-12"> <!-- " -12 " pour 1 élément -->
<?= $form->textarea('message', 'Votre message'); ?>
<!--<?= $form->text('check', ''); ?> -->
<?= $form->submit('Envoyer'); ?>
</div>
</div>
</form>

<!--<h2>Debug :</h2>
<?= var_dump($_SESSION); ?> -->

</div>
</div>

</body>
</html><?php
unset($_SESSION['inputs']);
unset($_SESSION['success']);
unset($_SESSION['errors']);
?>


Form.php
<?php
class Form {

private $datas = [];

public function __construct($datas = []){
$this-->datas = $datas;
}

private function getValue($name){
if(isset($this-->datas[$name])){
return $this->datas[$name];
}else{
return '';
}
}

private function input($type, $name, $label) {
$value = $this->getValue($name);
if($type == 'textarea') {
$input = "<textarea id=\"input$name\" class=\"form-control\" name=\"$name\">$value</textarea>";
}else{
$input = "<input id=\"input$name\" class=\"form-control\" type=\"$type\" name=\"$name\" value=\"$value\">";
}

return "<div class=\"form-group\">
<label for=\"input$name\">$Label</label>
$input
</div>";
}

public function text($name, $label) {
return $this->input('text', $name, $label);
}

public function email($name, $label) {
return $this->input('email', $name, $label);
}

public function input($name, $label, $options) {
$options_html = "";
$value = $this->getValue($name);
foreach($options as $k => $v){
$selected = '';
if($value == $k){
$selected = 'selected';
}
$options_html .= "<option value=\"$k\"$selected>$v</option>";
}

return "<div class=\"form-group\">
<label for=\"input$name\">$Label</label>
<select id=\"input$name\" class=\"form-control\" name=\"$name\">$options_html</select>
</div>";
}

public function textarea($name, $label) {
return $this->input('textarea', $name, $label);
}

public function submit($label) {
return '<button type="submit" class="btn btn-primary">' . $label . '</button>';
}

}
?>


_inc.php
<?php
session_start();

function dd($var){
var_dump($var);
die();
}

require 'class/Form.php';
require 'class/Validator.php';
?>


post_contact.php
<?php
require '_inc.php';
$errors = [];
$emails = ['contact@localhost', 'depanage@localhost', 'heimerdigner@localhost'];

$validator = new validator($_POST);
$validator->check('name', 'required');
$validator->check('email', 'required');
$validator->check('email', 'email');
$validator->check('message', 'required');
$validator->check('service', 'in', array_keys($emails));
$errors = $validator->errors();

if(!empty($errors)) {

$_SESSION['errors'] = $errors;
$_SESSION['inputs'] = $_POST;
header('location: index.php');

}else{

$_SESSION['success'] = 1;
$headers = 'FROM: ' . $_POST['email'];
mail($emails[$_POST['service']], 'Formulaire de contact de ' . $_POST['name'], $_POST['message'], $headers);
header('locaion: index.php');
}
?>



Validator.php
<?php
class Validator {

private $datas = [];
private $errors = [];

public function __construct($datas){
$tthis->daa = $datas;
}

public function check($name, $rule, $options = false){
$validator = "validate_$rule";
if(!$this->$validator($name, $options)){
$this->errors[$name] = "Le champs $name n'a pas été rempli correctement";
}
}

public function validate_required($name){
return array_key_exists($name, $this->datas) && $this->datas[$name] != '';
}

public function validate_email($name){
return array_key_exists($name, $this->datas) && filter_var($this->datas[$name], FILTER_VALIDATE_EMAIL);
}

public function validate_in($name, $values){
return array_key_exists($name, $this->datas) && in_array($this->datas[$name], $values);
}

public function errors(){
return $this->errors;
}

}
?>




1 réponse

Messages postés
23144
Date d'inscription
samedi 22 octobre 2005
Statut
Modérateur
Dernière intervention
11 juin 2021
2 235
lut;)
le genre de 'tuto' à ne pas suivre ...
le genre de truc à ne pas faire ...
merci de renouveler 'ta' demande avec des explications plus "techniques" dans ton cas.