samedi 28 janvier 2017

Checkbox array isn't being sent from form to PHP

Only the name and email are being sent, and not the array. Here is the form :

<form class="form-horizontal" name="sentMessage" id="contactForm">
                    <fieldset>

                        <!-- Form Name -->
                        <legend>Préinscription</legend>

                        <!-- Text input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="text">Nom</label>  
                          <div class="col-md-4">
                            <input id="name" name="Préinscription" type="text" placeholder="Veuillez entrer votre nom." class="form-control input-md" required="">

                          </div>
                        </div>

                        <div class="form-group">
                          <label class="col-md-4 control-label" for="email">Email</label>  
                          <div class="col-md-4">
                          <input id="email" name="Préinscription" type="email" placeholder="Veuillez entrer votre adresse mail." class="form-control input-md" required="">

                          </div>
                        </div>

                        <!-- Multiple Checkboxes -->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="checkboxes">Samedi 5 août</label>
                          <div class="col-md-4">
                          <div class="checkbox">
                            <label for="checkboxes-0">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes5-0" value="1">
                              Saint amour
                            </label>
                            </div>
                          <div class="checkbox">
                            <label for="checkboxes-1">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes5-1" value="2">
                              Polisse
                            </label>
                            </div>
                          </div>
                        </div>

                        <!-- Multiple Checkboxes -->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="checkboxes">Dimanche 6 août</label>
                          <div class="col-md-4">
                          <div class="checkbox">
                            <label for="checkboxes-0">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes6-0" value="1">
                              La vie d'Adèle
                            </label>
                            </div>
                          <div class="checkbox">
                            <label for="checkboxes-1">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes6-1" value="2">
                              De rouille et d'os
                            </label>
                            </div>
                          </div>
                        </div>

                        <!-- Multiple Checkboxes -->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="checkboxes">Lundi 7 août</label>
                          <div class="col-md-4">
                          <div class="checkbox">
                            <label for="checkboxes-0">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes7-0" value="1">
                              Bang gang
                            </label>
                            </div>
                          <div class="checkbox">
                            <label for="checkboxes-1">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes7-1" value="2">
                              Médecin de campagne
                            </label>
                            </div>
                          <div class="checkbox">
                            <label for="checkboxes-2">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes7-2" value="3">
                              Les innocentes
                            </label>
                            </div>
                          </div>
                        </div>

                        <!-- Multiple Checkboxes -->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="checkboxes">Mardi 8 août</label>
                          <div class="col-md-4">
                          <div class="checkbox">
                            <label for="checkboxes-0">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes8-0" value="1">
                              La loi du marché
                            </label>
                            </div>
                          <div class="checkbox">
                            <label for="checkboxes-1">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes8-1" value="2">
                              Les malheurs de sophie
                            </label>
                            </div>
                          <div class="checkbox">
                            <label for="checkboxes-2">
                              <input type="checkbox" name="checkboxes[]" id="checkboxes8-2" value="3">
                              Ma loute
                            </label>
                            </div>
                          </div>
                        </div>

                        <br/>
                        <!-- Button -->
                        <div id="success"></div>
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="singlebutton"></label>
                          <div class="col-md-4">
                            <button id="singlebutton" name="singlebutton" class="btn btn-default" type="submit">Envoyer</button>
                          </div>
                        </div>

                    </fieldset>
                </form>

And here is the PHP that goes with it :

<?php
// Check for empty fields
if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
   echo "No arguments Provided!";
   return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));

$filmsSelectionner= 'Aucun';
if(isset($_POST['checkboxes']) && is_array($_POST['checkboxes']) && count($_POST['checkboxes']) > 0){
    $filmsSelectionner= implode(', ', $_POST['checkboxes']);
}

// Create the email and send the message
$to = 'islam20088@hotmail.com';
$email_subject = "Nouvel preinscription de la part de $name";
$email_body = "Vous avez reçu une nouvelle préinscription depuis votre forumalaire sur le site de Les Films de Plein Air.\n\n"."Voici les détails:\n\nNom: $name\n\nEmail: $email_address\n\nFilms à laquel il y sera: $filmsSelectionner\n\n";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";   
mail($to,$email_subject,$email_body,$headers);
return true;         
?>

And in my email, both the text and the email are being sent. Also, the $filmsSelectionner is being sent as well and it's value is Aucun. That means the problem is coming from the if condition. I need help, I'm a beginner ! :)




1 commentaire:

  1. Kindly remove my email from this post or I'll have to report it for privacy issues.

    Kind regards,
    Islam

    RépondreSupprimer