0

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"

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
it4Astuces
  • 432
  • 5
  • 17

1 Answers1

1

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();
?>
Nadir Latif
  • 3,690
  • 1
  • 15
  • 24
  • Thanks for your answer @Nadir but I already did it, i forget to copy it in the code above. What i don't understand why it's working on localhost and not working on my web host. When i click the submit button, it sends the mail but i noticed that the page not reloaded to show the success division – it4Astuces Apr 04 '18 at 17:54
  • You may be getting the problem because the php settings on your local host are different from the ones on your web host. You can get more information about the error by enabling error reporting. You can do this by adding the lines error_reporting(E_ALL); ini_set("display_errors", '1'); to the top of your contact-us.php file before the session_start(); – Nadir Latif Apr 05 '18 at 06:54
  • 1
    Thank you so mach! I did it and that's what shows as an error: 1 Warning: session_start(): Cannot send session cache limiter - headers already sent 2 Warning: Cannot modify header information - headers already sent so i did my research and i found out the solutions of the 2 errors: https://stackoverflow.com/questions/8812754/cannot-send-session-cache-limiter-headers-already-sent https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – it4Astuces Apr 05 '18 at 23:16