0

I have an existing Contact Form that I got through the StartBoostrap template called Freelancer... It uses Javascript and sends that information via PHP file to my email.

EDIT : For the record I am not experienced in what I am about to do.

I want to add a checkbox option so people can let me know if they'd like to be put on a newsletter list... I am not a JS/PHP person, strictly front end.... I've gone through this and this but I can't seem to get them to work within my existing constraints and have the email show if the box was checked or not.

Here is my HTML :

<form name="sentMessage" id="contactForm" novalidate>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Name</label>
                                <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Email Address</label>
                                <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Phone Number</label>
                                <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>You've got questions, we've got answers!</label>
                                <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="We will answer your questions ASAP!"></textarea>
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <br>
                        <div id="success"></div>

                        <div class="row">
                            <div class="form-group col-xs-6">
                               <button type="submit" class="btn-contact btn-lg hover">SEND</button>
                            <div class="checkbox-inline">
                            <label><input type="checkbox" id="mail_list_checkbox">Join our mailing list!</label>
                            </div>      
                         </div>
                    </form>

Here is my JS :

    $(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            // Prevent spam click and default submit behaviour
            $("#btnSubmit").attr("disabled", true);
            event.preventDefault();

            // 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(' ');
            var checkbox = $("#mail_list_checkbox").val();
            $('#mail_list_checkbox').val();
            if ($('#mail_list_checkbox').is(":checked"))
            {
            // it is checked
            }
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message,
                    checkbox: checkbox
                },
                cache: false,
                success: function() {
                    // Enable button & show success message
                    $("#btnSubmit").attr("disabled", false);
                    $('#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>Your message has been sent. </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>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#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('');
});

Here is the PHP :

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

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$message = $_POST['checkbox'];

// Create the email and send the message
$to = 'me@mywebsite.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "Hey!! You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message\n\nDo they want to join the mailing list: $checkbox";
$headers = "From: me@mywebsite.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;            
?>

Thank you for your time.

SaintPaddy
  • 37
  • 5

1 Answers1

0

1) Your checkbox doesn't have a unique identifier. You need to add an ID or Class like the other inputs have (eg. the name input has an id="name" identifier which allows you to target it within JavaScript/PHP.)

2) Your JS file has the right idea with "checkbox: checkbox", but unfortunately, you're missing the checkbox variable. You forgot to get a value for the checkbox (see the lines below "// get values from FORM" -- you need to add a "var checkbox" and pull the data from the checkbox.val.) See Get checkbox value in jQuery for more information on how to do this.

3) Your PHP file, like the JS file, is missing checkbox information. Since there's no information for the checkbox being posted, you don't have a means to call the value. You need to define a checkbox variable that is tied to the POST data of the checkbox. No information is currently being pulled for the checkbox, and no information is being displayed in the body content that ties to the checkbox.

JavaScript/jQuery is considered front-end, so you should put a little more effort into figuring this out! Any seasoned front-end developer should be comfortable in simple jQuery tasks like this IMO.

The PHP script you're using is an extremely simple one, as well, and you can easily read through it to figure out how elements are being called, how they're showing up in emails that are being sent, etc.

Hope this helps.

Community
  • 1
  • 1
Ryne
  • 348
  • 2
  • 6
  • Thank you @Ryne .... I hope I didn't give you the impression that I was a seasoned front-end developer, I am certainly not. First time dabbling in Java&PHP, it's not my forte. I've been working on this overnight, and I can't seem to get the email to send. I've edited the PHP (I think it's correct) and the Javascript, but I'm not sure it's right... Could you give me a pointer? – SaintPaddy Jun 21 '16 at 14:49
  • Could you update your original code so I can take a look at the revisions you made? And I was just informing you :) Keep in mind JavaScript and Java are different.... front-end developers will typically know way more JS than pure Java. jQuery is a great library to start with and dip your toes into what you can manipulate on the client-side. – Ryne Jun 22 '16 at 19:22
  • thank you, the codes have been updated... In HTML the id "mail_list_checkbox" was added to the checkbox... In JS this was added (as you pointed out above) var checkbox = $("#mail_list_checkbox").val(); $('#mail_list_checkbox').val(); if ($('#mail_list_checkbox').is(":checked")) ... I've edited the PHP accordingly to what was shown there, as you said that was fairly simple. I'm a noob here too so if I have to highlight something, let me know. – SaintPaddy Jun 24 '16 at 00:31