How to send select element by email?

Asked

Viewed 532 times

1

I have a contact form that uses JS to validate and PHP to submit, works very well with inputs text and the like, I would like to implement select in it. I’ll pass my codes here, I’m afraid it’s too long:

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 to me, with all inputs duly filled, however the "SUBJECT" (select) is empty, someone could guide me?

1 answer

1


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

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

For this reason:

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

Or best:

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

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

I recommend studying the CSS selectore to understand how they work, the jQuery manual itself gives a notion:

  • Man, first of all thank you very much!! I don’t know how I didn’t think of it, Big Brother!! I was wondering if you can help me to get this template to send attached files to my email as well, such as images, etc. If it’s something more complex and you can send me tutorials or articles would be great too. Thank you again!! Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.