-1

To preface, this JS and PHP new territory to me. I don't dabble much in HTML much anymore (focusing on print design) but I've done a lot on this site.

I've got a contact form lifted from Bootstrap that I want to add a check box too (for the mailing list).

I want the checkbox to signal the users request to be included on the mailing list. The JavaScript is to check for the checkbox position (on/off) and spit that info to the PHP file which sends me an email to me (with the rest of the collected contact form info).

I know that the contact form was working before I added the checkbox information. I feel that the PHP code is fine (it feels pretty straightforward). I think the problem is in the JavaScript.

I've tried this solution, but I think I have to reconfigure my JS and PHP to make it work. I've tried to incorporate this solution and this solution.

Can anyone see what I'm doing wrong? I think the problem is in the JS as I feel the PHP is really straightforward.

HTML code:

<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>

JQuery (contact_me.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();
        $("input[type='checkbox']").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('');
});

PHP (contact_me.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 = 'info@mysite.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: info@mysite.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;            
?>
Community
  • 1
  • 1
SaintPaddy
  • 35
  • 5
  • so what problem you are facing or any `error` you get? – Anuj Khandelwal Jul 26 '16 at 18:35
  • you're assigning $message twice in your php... I'm thinking you want to assign $message = $_POST['message'] and on the next line do something like $checkbox = $_POST['checkbox']... That is all i can see that seems odd... more info on what expected behavior is and what the problem is would be more helpful... – Tim Jul 26 '16 at 19:12
  • @AnujKhandelwal I do not get any errors. When I installed the checkbox into HTML, it worked fine then. When I added the `$("input[type='checkbox']").val();` in JS, it stopped working (sending the contact info to my designated email.) – SaintPaddy Jul 26 '16 at 19:49
  • @Tim Thanks Tim, I'll adjust that.... My expected behaviour is that the checkbox indicates whether the user wants to be included on the mailing list. The Javascript is suppost to check for on/off of checkbox and the PHP collects that (with the rest of the Contact Form info) and sends my designated email address all that information. – SaintPaddy Jul 26 '16 at 19:52
  • i don't get why you add `$("input[type='checkbox']").val();` this and `$('#mail_list_checkbox').val();` this when you don't store them in any variable or use anywhere.. – Anuj Khandelwal Jul 26 '16 at 20:08
  • Hello @AnujKhandelwal I used that code [from here](http://stackoverflow.com/questions/2834350/get-checkbox-value-in-jquery?noredirect=1&lq=1) ... In your opinion how should I check the checkbox state, collect and send to PHP? – SaintPaddy Jul 26 '16 at 20:16
  • try to remove `$("input[type='checkbox']").val();` after `var message` and `$('#mail_list_checkbox').val();` this after `var checkbox` – Anuj Khandelwal Jul 26 '16 at 20:21
  • @AnujKhandelwal I've edited the above code down to `var checkbox = ($('#mail_list_checkbox').is(":checked"))` ... I do not get an email... It does not give me an error, but it does not send an email... Is that JS right? – SaintPaddy Jul 26 '16 at 21:24

0 Answers0