put checkbox value in the email?

Asked

Viewed 309 times

0

How do I place whether or not the user has checked the form?

Form:

<form id="form2"  method="post">
    <input type="text" placeholder="NOME" value="" name="nome1" id="nome1" />
    <input type="email" placeholder="EMAIL" value="" name="email1" id="email" />
    <input type="tel" placeholder="TELEFONE" value="" name="telefone1" id="nome1" />
    <div><input type="checkbox" name="two" value="desejo receber informações" /><label>Desejo receber informações e ofertas</label></div> //SERIA ESTE CHECKBOX
    <button type="submit" class="btn m-btn">ENVIAR<span class="fa fa-angle-right"></span></button>
</form>

PHP:

<?php
// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
echo json_encode(array('error'=>'true'));
return false;
}

$name = $_POST['name'];
$email_address = $_POST['email'];

$phone = $_POST['phone'];
$subject = ($_POST['subject'] ? $_POST['subject'] : "Website Contact Form:  $name");



// Create the email and send the message
$to = '[email protected]';// Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = $subject;
$email_body = "You have received a new message from your website contact form.\n"."Here are the details:\nName: $name\nLast Name: $lastname\nEmail: $email_address\nPhone: $phone\nMessage:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
echo json_encode(array('success'=>'true'));
return true;            
?>

script:

 $(function() {
$("input,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#nome1").val();
    var email = $("input#email1").val();
    var phone = $("input#telefone1").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-drive.php",
        type: "POST",
        dataType: 'json',
        data: {
            name: name,
            email: email,
            phone: phone,
            message: message
        },
        cache: false,
        success: function(data) {
            if(data.error){
                // 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("<span>Perdão " + firstName + ", parece que ocorreu uma falha no envio, tente novamente!</span>");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#form2').trigger("reset");
            }
            else if(data.success){
                // 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("<span>Sua mensagem foi enviada com sucesso </span>");
                $('#success > .alert-success').append('</div>');
                //clear all fields
                $('#form2').trigger("reset");
            }
        }
    })
},
filter: function() {
    return $(this).is(":visible");
},
});
});


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

2 answers

2


The way you’re doing I’d do it like this:

Add id to checkbox field

<input type="checkbox" name="two" id="two" value="desejo receber informações" />

In the script would add

var two = "N";
if($("#two").is(":checked")){
    two= "S";
}

Ai would add the variable two on the date

 data: {
     two:two,
     .... seguido das outras que ja estao
 }

Finally get in php

$two = $_POST['two'];

There is another way to do it just by creating all the form ai in php:

if(isset($_POST['two']){
   //pega o valor
}

2

First change the following in the html of your form:

I wish to receive information and offers

Change these lines of your PHP

$name = $_POST['name'];
$email_address = $_POST['email'];
$atualiza = $_POST['two'];
If (!$atualiza) {  
$atualiza = "Não";
     } else    {
    $atualiza = $atualiza;
    }

$email_body = "You have received a new message from your website contact form.\n"."Here are the details:\nName: $name\nLast Name: $lastname\nEmail: $email_address\nPhone: $phone\nMessage:\n$message" \Quer receber atualização:\n$atualiza";

See if it helps you!

  • Ai has not left the html here follows : '<div><input type="checkbox" checked="checked" name="two" value="yes" id="two" /><label>I wish to receive information and offers</label></div>'

Browser other questions tagged

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