PHP Contact Form多个复选框发送电子邮件

时间:2018-11-30 08:48:46

标签: php jquery html email

我刚开始使用PHP编码。目前,我在联系表单中提交的复选框和下拉框存在问题。 我正在使用Bootstrap构建站点。 我已经研究了答案,但无法找到为什么我的复选框不会显示在电子邮件中的原因。

下面是我的代码:

这是PHP:

if($_POST) {

// Enter the email where you want to receive the message
$emailTo = 'yourname@company.com';

$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);

}

以下是联系表格:

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

这是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);

});

1 个答案:

答案 0 :(得分:0)

如果要使用多个具有相同名称的复选框,则需要将其设置为数组-name="cylinderto[]"。参见getting multiple checkboxes names/id's with php

由于这些字段没有“ required”属性,因此在尝试将变量插入电子邮件之前,您应该确保已设置该变量。

此外,我在您的代码中都看不到任何上述“下拉框”。