-3

I'm fairly new to coding in PHP. Currently I am having a problem with my checkboxes and dropdown boxes submitting in my contact form. I am using Bootstrap to build the site. I've researched answers and I have not been able to find why my checkboxes won't show up in my email.

Below is my code:

Here is the PHP:

if($_POST) {

// Enter the email where you want to receive the message
$emailTo = '[email protected]';

$name = trim($_POST['name']);
$company = trim($_POST['company']);
$clientEmail = trim($_POST['email']);
$phone = trim($_POST['phone']);
$cylinderto = trim($_POST['cylinderto']);
$approxquantity = trim($_POST['approxquantity']);
$deliveryaddress = trim($_POST['deliveryaddress']);
$message = trim($_POST['message']);
$subject = "LPG For Business";

$errors_array = array('name' => '', 'company' => '', 'email' => '', 'phone' => '', 'cylinderto' => '', 'approxquantity' => '', 'deliveryaddress' => '', 'message' => '');

if($name == '') {
    $errors_array['name'] = 'Please enter your full name!';
}
if($company == '') {
    $errors_array['company'] = 'Please enter your company name!';
}
if(!isEmail($clientEmail)) {
    $errors_array['email'] = 'Insert a valid email address!';
}
if($phone == '') {
    $errors_array['phone'] = 'Please enter the phone no!';
}
if(($cylinderto == '') {
    $errors_array['cylinderto'] = 'Please select the CYLINDER TYPES TO ORDER!';
}
if($approxquantity == '') {
    $errors_array['approxquantity'] = 'Please enter quantity!';
}
if($deliveryaddress == '') {
    $errors_array['deliveryaddress'] = 'Please enter valid delivery address!';
}
if($message == '') {
    $errors_array['message'] = 'Please enter the message!';
}
if($message != '' && $name != '' && isEmail($clientEmail) && $subject != '') {
    // Send email
$headers = "From: " . $name . " <" . $clientEmail . ">" . "\r\n";
$body ="Full Name:\t\t" . $name. "\nCompany Name:\t\t" .$company. "\nEmail:\t\t" .$clientEmail. "\nPhone Number:\t" . $phone. "\CYLINDER TYPES TO ORDER:\t" . $cylinderto. "\nApproximate Quantity (weekly):\t" . $approxquantity. "\nDelivery Address:\t" . $deliveryaddress. "\nAdditional Comments:\t" . $message;
mail($emailTo, $subject , $body, $headers);

}
 echo json_encode($errors_array);

}

Here is the Contact Form:

<form id='lpg-for-business' novalidate="" role="form" class="form-lpg-bizz">
<div class="form-row">
    <div class="form-group md-input col-md-6">
        <input type="text" class="md-form-control" required="" name="name" id="name"> <span class="highlight"></span>
        <label for="name">Full Name</label> <small class="form-text text-danger text-right">required*</small>
    </div>
    <div class="form-group md-input col-md-6">
        <input type="text" class="md-form-control" required="" name="company" id="company"> <span class="highlight"></span>
        <label for="company">Company Name</label> <small class="form-text text-danger text-right">required*</small>
    </div>
</div>
<div class="form-group md-input">
    <input type="email" class="md-form-control" required="" name="email" id="email"> <span class="highlight"></span>
    <i class="icon-Envelope prefix"></i>
    <label for="email">Email Address</label> <small class="form-text text-danger text-right">required*</small>
</div>
<div class="form-group md-input">
    <input type="number" class="md-form-control" required="" name="phone" id="phone"> <span class="highlight"></span>
    <i class="icon-Phone-2 prefix"></i>
    <label for="phone">Phone</label> <small class="form-text text-danger text-right">required*</small>
</div>
<div class="form-group">
    <label class="single-lable">CYLINDER TYPES TO ORDER (multiple options)</label>
    <div class="inputGroup">
        <input id="cylinderto" name="cylinderto" value="9Kg" type="checkbox" />
        <label for="cylinderto">9Kg
            <br><small>LPG cylinder</small>
        </label>
    </div>
    <div class="inputGroup">
        <input id="cylinderto" name="cylinderto" value="12Kg" type="checkbox" />
        <label for="cylinderto">12Kg
            <br><small>LPG cylinder</small>
        </label>
    </div>
    <div class="inputGroup">
        <input id="cylinderto" name="cylinderto" value="15Kg" type="checkbox" />
        <label for="cylinderto">15Kg
            <br><small>LPG cylinder</small>
        </label>
    </div>
    <div class="inputGroup">
        <input id="cylinderto" name="cylinderto" value="18Kg" type="checkbox" />
        <label for="cylinderto">18Kg
            <br><small>LPG cylinder</small>
        </label>
    </div>
    <div class="inputGroup">
        <input id="cylinderto" name="cylinderto" value="45Kg" type="checkbox" />
        <label for="cylinderto">45Kg
            <br><small>LPG cylinder</small>
        </label>
    </div>
</div>
<div class="form-group md-input">
    <input type="number" class="md-form-control" required="" name="approxquantity" id="approxquantity"> <span class="highlight"></span>
    <label for="approxquantity">Approximate Quantity (weekly)</label>
</div>
<div class="form-group md-input">
    <input type="text" class="md-form-control" required="" name="deliveryaddress" id="deliveryaddress"> <span class="highlight"></span>
    <i class="icon-Map2 prefix"></i>
    <label for="deliveryaddress">Delivery Address</label>
</div>
<div class="form-group md-input">
    <textarea class="md-form-control" rows="4" required="" name="message" id="message"></textarea>
    <label for="message">Additional Comments</label>
</div>
<div class="sumbit-btn-wrap">
    <button type="submit" id="submit" class="btn btn-info-gradiant btn-md btn-arrow" data-animation="animated fadeInUp" data-toggle="collapse"><span>Just Send IT! <i class="ti-arrow-right"></i></span>
    </button>
</div>

Here is the JQuery:

$("#lpg-for-business").submit(function (e) {

    e.preventDefault();
    var btn = $('#submit');
    /*btn.button('loading');*/
    setTimeout(function () {

        // btn.button('reset');

        var b = 'border-error';
        var ap = 'animated-error pulse';
        var bap = 'border-error animated-error pulse';

        var n = '#name';
        var name = $("#name").val();

        var co = '#company';
        var company = $("#company").val();

        var e = '#email';
        var email = $("#email").val();

        var cn = '#phone';
        var phone = $("#phone").val();

        var cyl = '#cylinderto';
        var cylinderto = $("#cylinderto").val();

        var aq = '#approxquantity';
        var approxquantity = $("#approxquantity").val();

        var dadd = '#deliveryaddress';
        var deliveryaddress = $("#deliveryaddress").val();

        var msg = '#message';
        var message = $("#message").val();

        var dataString = '&name=' + name + '&company=' + company + '&email=' + email + '&phone=' + phone + '&cylinderto=' + cylinderto + '&approxquantity=' + approxquantity + '&deliveryaddress=' + deliveryaddress + '&message=' + message;

        function isValidEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        }

        if (name.length <= 1) {
            $(n).addClass(bap);
            setTimeout(function () {
                $(n).removeClass(ap);
            }, 1000);
        } else { $(n).removeClass(b);}

        if (company.length <= 1) {
            $(co).addClass(bap);
            setTimeout(function () {
                $(co).removeClass(ap);
            }, 1000);
        } else { $(co).removeClass(b);}

        if (isValidEmail(email) === false) { $(e).addClass(bap);
            setTimeout(function () {
                $(e).removeClass(ap);
            }, 1000);
        } else { $(e).removeClass(b);}

        if (phone.length <= 1) {
            $(cn).addClass(bap);
            setTimeout(function () {
                $(cn).removeClass(ap);
            }, 1000);
        } else { $(cn).removeClass(b);}

        if (cylinderto.length <= 1) {
            $(cyl).addClass(bap);
            setTimeout(function () {
                $(cyl).removeClass(ap);
            }, 1000);
        } else { $(cyl).removeClass(b);}

        if (approxquantity.length <= 1) {
            $(aq).addClass(bap);
            setTimeout(function () {
                $(aq).removeClass(ap);
            }, 1000);
        } else { $(aq).removeClass(b);}

        if (deliveryaddress.length <= 1) {
            $(dadd).addClass(bap);
            setTimeout(function () {
                $(dadd).removeClass(ap);
            }, 1000);
        } else { $(dadd).removeClass(b);}

        if (message.length <= 1) {
            $(msg).addClass(bap);
            setTimeout(function () {
                $(msg).removeClass(ap);
            }, 1000);
        } else { $(msg).removeClass(b);}

        if (isValidEmail(email) && (message.length > 1) && (phone.length > 1) && (deliveryaddress.length > 1) && (approxquantity.length > 1) && (cylinderto.length > 1) && (company.length > 1) && (name.length > 1)) {

            $.ajax({
                type: "POST",
                url: "php/send_bussines_lpg.php",
                data: dataString,
                success: function () {
                    $(btn).fadeOut(500);
                    $('.success').fadeIn(1000);
                    $(n,co,e,cn,cyl,aq,dadd,msg).removeClass(b);
                    setTimeout(redirectTo,5000);
                }
            });

        }
        return false;
    }, 800);

});
2
  • 1
    What is the question? Commented Nov 30, 2018 at 9:08
  • Create Minimal, Complete and Verifiable example instead of pasting wall of code without description Commented Nov 30, 2018 at 10:58

1 Answer 1

0

If you are going to use multiple checkboxes with the same name they need to be set as an array - name="cylinderto[]". See getting multiple checkboxes names/id's with php.

You should probably make sure the variable is set before trying to insert it into the email since those fields doesn't have a "required" attribute.

Also, I'm not seeing any of the aforementioned "dropdown boxes" in your code anywhere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.