2012-05-09 24 views
0

好吧我不是一個jQuery的PHP專業人士如此裸露與我...我想通過複選框的值到電子郵件,並有一個上傳的文件添加爲附件電子郵件。這是我到目前爲止(一切正常,但複選框被傳遞和文件上傳)...我需要的主要是複選框 - 文件上傳只是一個額外的 - 你也將如何發送到數據庫]通過多個複選框和文件上傳使用jQuery AJAX到PHP來發送電子郵件

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 = '[email protected]'; 
$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("[email protected]","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 = '[email protected]'; //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; 
} 
?> 
+0

除非你是一個非常糟糕配置PHP,應該沒有必要使用stripslashes,並且您不使用過濾器庫來驗證電子郵件地址等內容。 –

回答

1

爲了得到複選框值使用

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

,而不是

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

即你必須指定名稱的數組。

而在提交複選框的值將在一個數組中,並可以使用循環或其他東西的值。

+1

我正要發佈這個確切的答案。 :) – elclanrs

+0

@elclanrs:我遇到過很多次相同的問題..;) –

+0

因此,我添加了[]的HTML表單名稱如上所述......我用這個JS文件 - ** var checkboxValues = {'reg_checkPick []':[]}; $(「input:checked」)。each(function(){checkboxValues ['reg_checkPick []']。push($(this).val());}); ** 現在我得到「[object Object ]在電子郵件中...所以錯誤在哪裏?PHP文件或JS文件?似乎這隻能得到1/3通常解決。我試圖將所有文件拼湊起來供大家參考。 – RooksStrife

0

我希望這會幫助你。

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

一旦在項目中,我使用的地圖是這樣的: 我已經改變了,這樣就可以在代碼中使用首先,使表ID。

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

在你的js改變複選框的代碼。你仍然可以採取確切[名稱=「reg_checkPick」]下面的代碼,使之更加具體。

我已經在我的本地進行過測試。

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

你會得到警告的值,因爲這

例如:如果我有選擇的油漆和設計思路

畫圖,設計理念

相關問題