0

我正在尝试将复选框值传递给电子邮件,并将上传的文件作为附件添加到电子邮件中。到目前为止,这是我所拥有的(一切正常,但是复选框被传递和文件上传)......我需要的主要是复选框 - 文件上传只是一个额外的 - 你也将如何将它发送到数据库;]

HTML 表单

<form id="reg-form" class="cols" method="post" action="#">  
    <fieldset class="w50">                                                        
        <p class="form-subscr-field">
            <label for="reg_first" id="reg_first_label">First Name: <span style="color:#F00">*</span></label>
            <input id="reg_first" type="text" name="reg_first" class="inputbox" size="10" tabindex="1" />
            <label class="error error-tip" for="reg_first" id="first_error" style="display: none;">Please enter your First Name</label>
        </p>
        <p class="form-subscr-field">
            <label for="reg_email" id="reg_email_label">E-mail Address: <span style="color:#F00">*</span></label>
            <input id="reg_email" type="text" name="reg_email" class="inputbox" size="10" tabindex="3" />
            <label class="error error-tip" for="reg_email" id="email_error" style="display: none;">Please enter your Email Address</label>
            <label class="error error-tip" for="reg_email" id="invalid_error" style="display: none;">Invalid Email Address.</label>
            </p>
        <p class="form-subscr-field">
            <label for="reg_zip" id="reg_zip_label">Zip Code: <span style="color:#F00">*</span></label>
            <input id="reg_zip" type="text" name="reg_zip" class="inputbox"  size="10" tabindex="5" />
            <label class="error error-tip" for="reg_zip" id="zip_error" style="display: none;">Please enter your Zip Code</label>
            <label class="error error-tip" for="reg_zip" id="invalid_error2" style="display: none;">Invalid Zip Code.</label>
        </p>
        <p class="form-subscr-field">
            <label for="reg_company" id="reg_company_label">Company Name: </label>
            <input id="reg_company" type="text" name="reg_company" class="inputbox" size="10" tabindex="1" />
        </p>
    </fieldset>

    <fieldset class="w50">
        <p class="form-subscr-field">
            <label for="reg_last" id="reg_last_label">Last Name: <span style="color:#F00">*</span></label>
            <input id="reg_last" type="text" name="reg_last" class="inputbox" size="10" tabindex="2" />
            <label class="error error-tip" for="reg_last" id="last_error" style="display: none; width:200px;">Please enter your Last Name</label>
        </p>
        <p class="form-subscr-field">
            <label for="reg_phone" id="reg_phone_label">Phone Number: <span style="color:#F00">*</span></label>
            <input id="reg_phone" type="text" name="reg_phone" class="inputbox" size="10" maxlength="20" tabindex="4" />
            <label class="error error-tip" for="reg_phone" id="phone_error" style="display: none; width:200px;">Please enter your Phone Number</label>
        </p>
        <p class="form-subscr-field">
            <label for="reg_areyou" id="reg_areyou_label">Are you&hellip;</label>
            <select id="reg_areyou" type="text" name="reg_areyou" class="inputbox" tabindex="6">
                <option value="Opt1">Select an option</option>
                <option value="Homeowner">Homeowner</option>
                <option value="Contractor">Contractor</option>
                <option value="Commercial Buyer">Commercial Buyer</option>
                <option value="Other">Other</option>
            </select>
        </p>
        <p class="form-subscr-field">
            <label for="reg_upload" id="reg_upload_label">Do you have a detailed file or image for your project - Upload it now! </label>
            <input name="reg_upload" id="reg_upload" type="file" style="cursor:pointer;">
        </p>
    </fieldset>
    <div class="clear"></div><br/>
    <h3 class="underline"><strong>Project</strong> Information</h3> 
    <label>Are you looking into doing any of the following?</label>       
        <table cellpadding="5" cellspacing="5" border="1" width="100%">
            <tr>
                <td align="left" valign="top"><input name="reg_checkPick" id="Decking" value="Decking" type="checkbox"><label>Decking</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Windows" value="Windows" type="checkbox"><label>Windows</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Doors" value="Doors" type="checkbox"><label>Doors</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Flooring" value="Flooring" type="checkbox"><label>Flooring</label></td>
            </tr>
            <tr>
                <td align="left" valign="top"><input name="reg_checkPick" id="Countertops" value="Countertops" type="checkbox"><label>Countertops</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Cabinets" value="Cabinets" type="checkbox"><label>Cabinets</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Sink and Faucets" value="Sink and Faucets" type="checkbox"><label>Sink and Faucets</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Siding" value="Siding" type="checkbox"><label>Siding</label></td>
            </tr>
            <tr>
                <td align="left" valign="top"><input name="reg_checkPick" id="Roofing" value="Roofing" type="checkbox"><label>Roofing</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Paint" value="Paint" type="checkbox"><label>Paint</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Hardware" value="Hardware" type="checkbox"><label>Hardware</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Fireplaces" value="Fireplaces" type="checkbox"><label>Fireplaces</label></td>
            </tr>
            <tr>
                <td align="left" valign="top"><input name="reg_checkPick" id="Installation" value="Installation" type="checkbox"><label>Installation</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Design Ideas" value="Design Ideas" type="checkbox"><label>Design Ideas</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Furnishings" value="Furnishings" type="checkbox"><label>Furnishings</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Plumbing" value="Plumbing" type="checkbox"><label>Plumbing</label></td>
            </tr>
            <tr>
                <td align="left" valign="top"><input name="reg_checkPick" id="Lumber" value="Lumber" type="checkbox"><label>Lumber</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Specialty Rooms" value="Specialty Rooms" type="checkbox"><label>Specialty Rooms</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Trusses" value="Trusses" type="checkbox"><label>Trusses</label></td>
                <td align="left" valign="top"><input name="reg_checkPick" id="Mill Work" value="Mill Work" type="checkbox"><label>Mill Work</label></td>
            </tr>
        </table>
    <p class="form-subscr-field">
        <label for="reg_message" id="comment_label">Project Details: <span style="color:#F00">*</span></label>
        <textarea id="reg_message" name="reg_message" rows="8" cols="30" tabindex="8" placeholder="Please provide as much information and details that you can."></textarea>
        <label class="error error-tip" for="reg_message" id="message_error" style="display: none;">Please enter Your Message</label>
    </p>
    <p class="form-subscr-field">
        <label for="reg_human" id="reg_human_label">Please add <strong>2 + 2</strong> to verify your human! <span style="color:#F00">*</span></label>
        <input id="reg_human" type="text" name="reg_human" class="inputbox" maxlength="1" size="1" placeholder="Hint - it's 4" tabindex="9" />
        <label class="error error-tip" for="reg_human" id="human_error" style="display: none;">You need to enter 4.</label>
        <label class="error error-tip" for="reg_human" id="invalid_error3" style="display: none;">Your math is off.</label>
    </p>
    <div class="clear"></div>
    <div class="submit-contact">
        <p><input type="submit" class="cool-button2 csbutton spot-action" value="Submit" tabindex="10" /> <button type="reset" class="csbutton-color" tabindex="11">Reset</button></p> 
    </div>
</form>

JS文件

//quote form validation
$(function () {$('.error').hide(); $(".cool-button2").click(function () {$('.error').hide(); $("label#invalid_error").hide(); $("label#email_error").hide();
    var first2 = $("input#reg_first").val(); if (first2 === "") {$("label#first_error").show(); $("input#reg_first").focus(); return false; }
    var last2 = $("input#reg_last").val(); if (last2 === "") {$("label#last_error").show(); $("input#reg_last").focus(); return false; }
    var email2 = $("input#reg_email").val(); if (email2 === "") {$("label#email_error").show(); $("input#reg_email").focus(); return false; }
    var emailRegEx2 = /^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/;
    var phone2 = $("input#reg_phone").val(); if (phone2 === "") {$("label#phone_error").show(); $("input#reg_phone").focus(); return false; }
    var zip2 = $("input#reg_zip").val(); if (zip2 === "") {$("label#zip_error").show(); $("input#reg_zip").focus(); return false; }
    var zipRegEx2 = /^\d{5}(-\d{4})?$/;
    var areyou2 = $('#reg_areyou option:selected').text();
    var company2 = $("input#reg_company").val();
    var checkboxValues = new Array(); $("input[name='reg_checkPick']:checked").each(function(i) {data.push($(this).val());});
    var message2 = $("textarea#reg_message").val(); if (message2 === "") {$("label#message_error").show(); $("textarea#reg_message").focus(); return false; }
    var human2 = $("input#reg_human").val(); if (human2 === "") {$("label#human_error").show(); $("input#reg_human").focus(); return false; }
    var humanRegEx2 = 4;

    if (document.getElementById('reg_email').value.search(emailRegEx2) === -1) { $("label#invalid_error").show(); $("input#reg_email").focus(); }
    else if (document.getElementById('reg_zip').value.search(zipRegEx2) === -1) { $("label#invalid_error2").show(); $("input#reg_zip").focus(); }
    else if (document.getElementById('reg_human').value.search(humanRegEx2) === -1) { $("label#invalid_error3").show(); $("input#reg_human").focus(); }
    else {
        var dataString = 'reg_first=' + first2 + '&reg_last=' + last2 + '&reg_email=' + email2 + '&reg_phone=' + phone2 + '&reg_zip=' + zip2 + '&reg_areyou=' + areyou2 + '&reg_company=' + company2 + '&reg_checkPick=' + checkboxValues + '&reg_message=' + message2; $.ajax({type: "POST", url: "reg.php", data: dataString, success: function () {$('#reg-form').html("<div id='message2'></div>"); $('#message2').html("<h3>Request Submitted</h3>").append("<p>Thank you for contacting us - we will be in touch. If you have any further questions, you can always mail us at <a href=\"mailto:\">notmine</a> or call our support team at Ph: 00000000.</p>").hide().fadeIn(1500, function () {$('#message2').append("<img id='checkmark' src='/submit.png' />"); }); }}); }
    return false; }); });

PHP 文件

<?php
if ((isset($_POST['reg_first'])) && (strlen(trim($_POST['reg_first'])) > 0)) {
    $reg_first = stripslashes(strip_tags($_POST['reg_first']));
} else {$reg_first = 'No First Name entered';}

if ((isset($_POST['reg_last'])) && (strlen(trim($_POST['reg_last'])) > 0)) {
    $reg_last = stripslashes(strip_tags($_POST['reg_last']));
} else {$reg_last = 'No Last Name entered';}

if ((isset($_POST['reg_email'])) && (strlen(trim($_POST['reg_email'])) > 0)) {
    $reg_email = stripslashes(strip_tags($_POST['reg_email']));
} else {$reg_email = 'No email entered';}

if ((isset($_POST['reg_phone'])) && (strlen(trim($_POST['reg_phone'])) > 0)) {
    $reg_phone = stripslashes(strip_tags($_POST['reg_phone']));
} else {$reg_phone = 'No Phone Number entered';}

if ((isset($_POST['reg_areyou'])) && (strlen(trim($_POST['reg_areyou'])) > 0)) {
    $reg_areyou = stripslashes(strip_tags($_POST['reg_areyou']));
} else {$reg_areyou = 'No selection';}

if ((isset($_POST['reg_zip'])) && (strlen(trim($_POST['reg_zip'])) > 0)) {
    $reg_zip = stripslashes(strip_tags($_POST['reg_zip']));
} else {$reg_zip = 'No Zip Code entered';}

if ((isset($_POST['reg_company'])) && (strlen(trim($_POST['reg_company'])) > 0)) {
    $reg_company = stripslashes(strip_tags($_POST['reg_company']));
} else {$reg_company = 'No Company Name entered';}

if ((isset($_POST['reg_checkPick'])) && (strlen(trim($_POST['reg_checkPick'])) > 0)) {
    $reg_checkPick = stripslashes(strip_tags($_POST['reg_checkPick']));
} else {$reg_checkPick = 'Noting was checked';}

if ((isset($_POST['reg_message'])) && (strlen(trim($_POST['reg_message'])) > 0)) {
    $reg_message = stripslashes(strip_tags($_POST['reg_message']));
} else {$reg_message = 'No message entered';}

ob_start();
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="600" border="1" cellspacing="2" cellpadding="2">
  <tr bgcolor="#eeffee">
    <td>First Name</td>
    <td>Last Name</td>
    <td>Email</td>
    <td>Phone Number</td>
    <td>Zip Code</td>
    <td>Are You</td>
    <td>Company Name</td>
    <td>Looking into doing the following</td>
    <td>Message</td>
  </tr>
  <tr bgcolor="#eeeeff">
    <td><?=$reg_first;?></td>
    <td><?=$reg_last;?></td>
    <td><?=$reg_email;?></td>
    <td><?=$reg_phone;?></td>
    <td><?=$reg_zip;?></td>
    <td><?=$reg_areyou;?></td>
    <td><?=$reg_company;?></td>
    <td><?=$reg_checkPick;?></td>
    <td><?=$reg_message;?></td>
  </tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();

$to = 'notmine@hotmail.com';
$email = $reg_email;
$fromaddress = $reg_email;
$fromname = "Request a Quote";

require("phpmailer.php");

$mail = new PHPMailer();

$mail->From     = $reg_email;
$mail->FromName = ("$reg_first $reg_last");
$mail->AddAddress("notmine@hotmail.com","not mine");      //change to your email address

$mail->WordWrap = 50;
$mail->IsHTML(true);

$mail->Subject  =  "Request a Quote: form submitted";
$mail->Body     =  $body;
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send()) {
    $recipient = 'notmine@hotmail.com';    //change to your email address
    $subject = 'reg Info Form: form failed';
    $content = $body;   
  mail($recipient, $subject, $content, "From: $reg_email\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
  exit;
}
?>
4

2 回答 2

1

用于获取复选框值使用

<input name="reg_checkPick[]" id="Countertops" value="Countertops" type="checkbox">

代替

<input name="reg_checkPick" id="Countertops" value="Countertops" type="checkbox">

即您必须将名称指定为数组。

并且在提交复选框值将在一个数组中,并且可以使用循环或其他方式获取值。

于 2012-05-09T19:25:36.033 回答
0

我希望这能帮到您。

$(':checkbox').map(function() {
  return this.id;
}).get().join(',');   

一旦在我的项目中,我使用了这样的地图:我已经更改,以便您可以在代码中使用首先,使用 id 制作表格。

<table id='checkboxes' cellpadding='5' cellspacing='5' border='1' width='100%'>

在您的 js 中更改复选框代码。您仍然可以将确切的 [name='reg_checkPick'] 用于下面的代码以使其更具体。

我在本地测试过。

var selected = $("#checkboxes input:checked").map(function(i,el){
return $(this).val();}).get();
 alert(selected);   

您将获得警报中的值,因为

例如:如果我选择了绘画和设计理念

油漆,设计理念

于 2012-05-14T14:00:09.507 回答