复选框数组没有从表单发送到PHP

时间:2017-01-28 21:44:58

标签: php html forms checkbox

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>

以下是与之相关的PHP:

<?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 = 'xxx@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;         
?>

以下是用于发送表单的AJAX:

// Contact Form Scripts

$(function() {

    $("#contactForm input,#contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Votre message a bien été envoyé. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Désolé" + firstName + ", il semble que le serveur de messagerie ne répond plus. Veuillez réessayer plus tard!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

在我的电子邮件中,正在发送文本和电子邮件。此外,$ filmsSelectionner也被发送,它的价值是Aucun。这意味着问题来自if条件。我需要帮助,我是初学者! :)

0 个答案:

没有答案