How to send select element by email?

1

I have a contact form that uses JS to validate and PHP to send, works very well with input text and related, I would implement the select on it. I'm going to pass my codes here, I fear it will be very extensive:

Form:

<form name="sentMessage" id="contactForm" novalidate>
                <div class="control-group form-group">
                    <div class="controls col-lg-4">
                        <label>Nome Completo*</label>
                        <input type="text" class="form-control" id="name" required data-validation-required-message="Por favor, insira seu nome.">
                        <p class="help-block"></p>
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls col-lg-3">
                        <label>Telefone*</label>
                        <input type="tel" class="form-control" id="phone" required data-validation-required-message="Por favor, insira seu número de telefone." placeholder="(00) 0000-0000">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls col-lg-5">
                        <label>Email*</label>
                        <input type="email" class="form-control" id="email" required data-validation-required-message="Por favor, insira seu email.">
                    </div>
                </div>


               </div><!-- row -->

                <hr>

                <div class="row">
                <div class="control-group form-group">
                    <div class="controls col-lg-3">



                        <div class="form-group">
                 <label style="padding-top:15px" for="sel1">Assunto:</label>
                   <select class="form-control" id="sel1">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                   </select>
           </div>



                    </div>
                </div>

                </div><!-- row -->
                <br>
                <hr>
                <div class="row">
                <div class="control-group form-group">
                    <div class="controls col-lg-12">

                        <label>Mensagem</label>
                        <textarea rows="10" cols="100" class="form-control" id="message" maxlength="999" style="resize:none"></textarea>
                    </div>
                </div>

                </div><!--row-->


                <div class="row">
                 <div class="col-lg-5">
                <div id="success"></div>
                <!-- For success/fail messages -->
                <br>
                <button type="submit" class="btn btn-success btn-lg">Enviar</button>
                </div>
            </form>

        </div>

Validator javascript:

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {

        },
        submitSuccess: function($form, event) {
            event.preventDefault(); 
            var name = $("input#name").val();
            var phone = $("input#phone").val();
            var email = $("input#email").val();
        var sel1 = $("input#sel1").val();
            var message = $("textarea#message").val();
            var firstName = name; 
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "bin/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
            sel1: sel1,
            message: message
                },

php:

<?php if(empty($_POST['name'])          ||
   empty($_POST['phone'])       ||
   empty($_POST['email'])       ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$sel1 = $_POST['sel1'];
$message = $_POST['message'];


$to = '[email protected]'; 
$email_subject = "Formulário de contato, $name te enviou uma mensagem."; 
$email_body = "Você recebeu uma nova mensagem do site.\n\n"."Aqui estão os detalhes:\n\nNome: $name\n\nTelefone: $phone\n\nAssunto: $sel1\n\nEmail: $email_address\n\nMensagem:\n$message";
$headers = "From: $email_address\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

The email arrives correctly for me, with all the inputs filled in, but the "SUBJECT" (select) is empty, could anyone guide me?

    
asked by anonymous 23.08.2015 / 03:29

1 answer

1

Your input#sel1 selector asks for a <input> instead of a <select> , just change this:

        var name = $("input#name").val();
        var phone = $("input#phone").val();
        var email = $("input#email").val();
        var sel1 = $("input#sel1").val(); //Está linha

To this:

        var name = $("input#name").val();
        var phone = $("input#phone").val();
        var email = $("input#email").val();
        var sel1 = $("select#sel1").val();

Or better :

        var name = $("#name").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var sel1 = $("#sel1").val();

# is used to get elements with id in html, remember that ids can never be repeated, so I omitted in the last example the input and select and left only #

I recommend that I study the CSS selectors to understand how they work, the jQuery manual itself gives a notion:

23.08.2015 / 03:40