AJAX 发布到 PHP 复选框未显示在电子邮件中

发布于 2024-12-10 04:19:51 字数 8290 浏览 0 评论 0原文

我遇到了障碍,并且已经坚持了一段时间,但没有成功。 Jquery 采用 html 表单,对其进行验证,然后使用 AJAX 发布到通过电子邮件发送结果的 php 脚本。

HTML 片段:

<label>&nbsp;</label>
        <input type="checkbox" name="insurancetype[]" value="Life Insurance" />Life Insurance
        <div class="clear"></div>

        <label>&nbsp;</label>
        <input type="checkbox" name="insurancetype[]" value="Income Protection" />Income Protection         
        <div class="clear"></div>

        <label>&nbsp;</label>
        <input type="checkbox" name="insurancetype[]" value="Total and Permanent Disability" />Total and Permanent Disability
        <div class="clear"></div>

        <label>&nbsp;</label>
        <input type="checkbox" name="insurancetype[]" value="Trauma" />Trauma
        <div class="clear"></div>

JS 代码:

$(document).ready(function(){

    $("#maillifeinsurance").click(function(){
    var valid = '';
    var isr = ' is required';
    var fname = $("#fname").val();
    var lname = $("#lname").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var gender = $("#gender").val();
    var month = $("#month").val();
    var year = $("#year").val();
    var day = $("#day").val();      
    var smoking = $("#smoking").val();
    var occupation = $("#occupation").val();
    var income = $("#income").val();            
    var text = $("#text").val();
    var occupationdesc = $("#occupationdesc").val();

    var selectedinsurance = new Array();
    $("input[name='insurancetype']:checked").each(function(i) {
            selectedinsurance.push($(this).val());
    });


    if (fname.length<1) {
        valid += '<br /><strong>First Name</strong>'+isr;
    }
    if (lname.length<1) {
        valid += '<br /><strong>Last Name</strong>'+isr;
    }
    if (!phone.match(/\d{10}/)) {
        valid += '<br /><strong>A valid Phone Number</strong>'+isr+' with no spaces ex. <strong>XXXXXXXXXX</strong> or <strong>XXXX XXX XXX</strong>';
    }
    if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
        valid += '<br /><strong>A valid Email</strong>'+isr+' ex. <strong>[email protected]</strong>';
    }
    if (gender.length<1) {
        valid += '<br /><strong>Gender</strong>'+isr;
    }
    if (smoking.length<1) {
        valid += '<br /><strong>Smoking Status</strong>'+isr;
    }
    if (occupationdesc.length<1) {
        valid += '<br /><strong>Occupation Description</strong>'+isr+' ex. <strong>Selling Bicycle Products</strong>';
    }       
    if (occupation.length<1) {
        valid += '<br /><strong>Occupation Type</strong>'+isr;
    }
    if (income.length<1) {
        valid += '<br /><strong>Taxable Income</strong>'+isr+' ex. <strong>$50,000</strong>';
    }       
    if (day.length<1) {
        valid += '<br /><strong>Birth Day</strong>'+isr;
    }           
    if (month.length<1) {
        valid += '<br /><strong>Birth Month</strong>'+isr;
    }
    if (year.length<1) {
        valid += '<br /><strong>Birth Year</strong>'+isr;
    }
    if (valid!='') {
        $("#response").fadeIn("slow");
        $("#response").html("<strong>Submission Errors:</strong>"+valid);
    }
    else {
        var datastr ='fname=' + fname + '&lname=' + lname + '&email=' + email + '&phone=' + phone + '&gender=' + gender + '&occupationdesc=' + occupationdesc + '&smoking=' + smoking + '&occupation=' + occupation + '&day=' + day + '&month=' + month + '&year=' + year + '&income=' + income + '&text=' + text + '&selectedinsurance=' + selectedinsurance;
        $("#response").css("display", "block");
        $("#response").html("Sending message ... ");
        $("#response").fadeIn("slow");
        setTimeout("send('"+datastr+"')",2000);
    }
    return false;
});

});
function send(datastr){
$.ajax({    
    type: "POST",
    url: "mail-life-insurance.php",
    data: datastr,
    cache: false,
    success: function(html){
    $("#response").fadeIn("slow");
    $("#response").html(html);
    setTimeout('$("#response").fadeOut("slow")',2700);
    $("#response").fadeIn("slow");
    $("#response").html("<strong>Thank You</strong> Please Check Your Email Inbox.");
    $("#response").fadeIn("slow");
}
});
}

PHP 脚本:

      if(isset($_REQUEST[email])){
          $mail = $_REQUEST[email];
          $phone = $_REQUEST[phone];
          $fname = $_REQUEST[fname];
          $lname = $_REQUEST[lname];
          $day = $_REQUEST[day];
          $month = $_REQUEST[month];
          $year = $_REQUEST[year];
          $smoking = $_REQUEST[smoking];
          $occupationdesc = $_REQUEST[occupationdesc];
          $occupation = $_REQUEST[occupation];
          $gender = $_REQUEST[gender];
          $insured = $_REQUEST[insured];
          $income = $_REQUEST[income];
          $text = $_REQUEST[text];

          $insurancetype = $_REQUEST[selectedinsurance];
          $how_many = count($insurancetype);

      }

      $to = $mail;
      $subject = "Life Insurance, Income Protection, Trauma & TPD Quote";

      $message = "<h1>".$subject."</h1>";
      $message.="You Submitted The Following Information: <br /><br />";
      $message.="<strong>Full Name:</strong> ".$fname . " " . $lname;
      $message.="<br /><strong>Email:</strong> ".$mail;
      $message.="<br /><strong>Phone:</strong> ".$phone;
      $message.="<br /><strong>Gender:</strong> ".$gender;          
      $message.="<br /><strong>Smoking Status:</strong> ".$smoking;  
      $message.="<br /><strong>Date of Birth:</strong> " . $day . " / " . $month . " / " . $year;          
      $message.="<br /><strong>Occupation Description:</strong> ".$occupationdesc;           
      $message.="<br /><strong>Occupation Type:</strong> ".$occupation;
      $message.="<br /><strong>Taxable Income:</strong> ".$income;
      $message.="<br /><strong>Insurance Type Required:</strong> ";

      $message.= 'Insurance Type chosen: '.$how_many.'<br><br>';
    if ($how_many>0) {
        $message.= 'You chose the following Insurance Types:<br>';
    }
    for ($i=0; $i<$how_many; $i++) {
       $message.= ($i+1) . '- ' . $insurancetype[$i] . '<br>';
    }
        $message.= "\n\n";


      $message.="<br /><strong>Additional Information:</strong> ".$text;
      $message.="<br /><br />You will be contacted shortly to discuss your application for Business Insurance, if any details need changing please contact BUSINESS NAME by <strong>Calling PHONE</strong>";

      $temp = file_get_contents("cover.txt");
      $message = str_replace("[ALL_DETAILS]",$message,$temp);

      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $headers.= "From : BUSINESS NAME <[email protected]>\r\n";
      $headers.= "bcc :[email protected]\r\n";

      mail($to,$subject,$message,$headers);

    if(mail($to, $subject, $message)){
        echo "Your Message Has Sent";
    } 
    else{ 
        echo "there's some errors to send the mail, verify your server options";
    }

?>

我只是无法在收到的电子邮件中显示这些复选框。如果JS不验证它会起作用,但我需要在提交之前验证。此时头很痛,很绝望!非常感谢帮助,干杯米克

I've hit a snag and have been stuck on this for a little while now with no success. Jquery is taking the html form, validating it then posting using AJAX to a php script that emails out the results.

HTML Snippet:

<label> </label>
        <input type="checkbox" name="insurancetype[]" value="Life Insurance" />Life Insurance
        <div class="clear"></div>

        <label> </label>
        <input type="checkbox" name="insurancetype[]" value="Income Protection" />Income Protection         
        <div class="clear"></div>

        <label> </label>
        <input type="checkbox" name="insurancetype[]" value="Total and Permanent Disability" />Total and Permanent Disability
        <div class="clear"></div>

        <label> </label>
        <input type="checkbox" name="insurancetype[]" value="Trauma" />Trauma
        <div class="clear"></div>

JS Code:

$(document).ready(function(){

    $("#maillifeinsurance").click(function(){
    var valid = '';
    var isr = ' is required';
    var fname = $("#fname").val();
    var lname = $("#lname").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var gender = $("#gender").val();
    var month = $("#month").val();
    var year = $("#year").val();
    var day = $("#day").val();      
    var smoking = $("#smoking").val();
    var occupation = $("#occupation").val();
    var income = $("#income").val();            
    var text = $("#text").val();
    var occupationdesc = $("#occupationdesc").val();

    var selectedinsurance = new Array();
    $("input[name='insurancetype']:checked").each(function(i) {
            selectedinsurance.push($(this).val());
    });


    if (fname.length<1) {
        valid += '<br /><strong>First Name</strong>'+isr;
    }
    if (lname.length<1) {
        valid += '<br /><strong>Last Name</strong>'+isr;
    }
    if (!phone.match(/\d{10}/)) {
        valid += '<br /><strong>A valid Phone Number</strong>'+isr+' with no spaces ex. <strong>XXXXXXXXXX</strong> or <strong>XXXX XXX XXX</strong>';
    }
    if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
        valid += '<br /><strong>A valid Email</strong>'+isr+' ex. <strong>[email protected]</strong>';
    }
    if (gender.length<1) {
        valid += '<br /><strong>Gender</strong>'+isr;
    }
    if (smoking.length<1) {
        valid += '<br /><strong>Smoking Status</strong>'+isr;
    }
    if (occupationdesc.length<1) {
        valid += '<br /><strong>Occupation Description</strong>'+isr+' ex. <strong>Selling Bicycle Products</strong>';
    }       
    if (occupation.length<1) {
        valid += '<br /><strong>Occupation Type</strong>'+isr;
    }
    if (income.length<1) {
        valid += '<br /><strong>Taxable Income</strong>'+isr+' ex. <strong>$50,000</strong>';
    }       
    if (day.length<1) {
        valid += '<br /><strong>Birth Day</strong>'+isr;
    }           
    if (month.length<1) {
        valid += '<br /><strong>Birth Month</strong>'+isr;
    }
    if (year.length<1) {
        valid += '<br /><strong>Birth Year</strong>'+isr;
    }
    if (valid!='') {
        $("#response").fadeIn("slow");
        $("#response").html("<strong>Submission Errors:</strong>"+valid);
    }
    else {
        var datastr ='fname=' + fname + '&lname=' + lname + '&email=' + email + '&phone=' + phone + '&gender=' + gender + '&occupationdesc=' + occupationdesc + '&smoking=' + smoking + '&occupation=' + occupation + '&day=' + day + '&month=' + month + '&year=' + year + '&income=' + income + '&text=' + text + '&selectedinsurance=' + selectedinsurance;
        $("#response").css("display", "block");
        $("#response").html("Sending message ... ");
        $("#response").fadeIn("slow");
        setTimeout("send('"+datastr+"')",2000);
    }
    return false;
});

});
function send(datastr){
$.ajax({    
    type: "POST",
    url: "mail-life-insurance.php",
    data: datastr,
    cache: false,
    success: function(html){
    $("#response").fadeIn("slow");
    $("#response").html(html);
    setTimeout('$("#response").fadeOut("slow")',2700);
    $("#response").fadeIn("slow");
    $("#response").html("<strong>Thank You</strong> Please Check Your Email Inbox.");
    $("#response").fadeIn("slow");
}
});
}

PHP Script:

      if(isset($_REQUEST[email])){
          $mail = $_REQUEST[email];
          $phone = $_REQUEST[phone];
          $fname = $_REQUEST[fname];
          $lname = $_REQUEST[lname];
          $day = $_REQUEST[day];
          $month = $_REQUEST[month];
          $year = $_REQUEST[year];
          $smoking = $_REQUEST[smoking];
          $occupationdesc = $_REQUEST[occupationdesc];
          $occupation = $_REQUEST[occupation];
          $gender = $_REQUEST[gender];
          $insured = $_REQUEST[insured];
          $income = $_REQUEST[income];
          $text = $_REQUEST[text];

          $insurancetype = $_REQUEST[selectedinsurance];
          $how_many = count($insurancetype);

      }

      $to = $mail;
      $subject = "Life Insurance, Income Protection, Trauma & TPD Quote";

      $message = "<h1>".$subject."</h1>";
      $message.="You Submitted The Following Information: <br /><br />";
      $message.="<strong>Full Name:</strong> ".$fname . " " . $lname;
      $message.="<br /><strong>Email:</strong> ".$mail;
      $message.="<br /><strong>Phone:</strong> ".$phone;
      $message.="<br /><strong>Gender:</strong> ".$gender;          
      $message.="<br /><strong>Smoking Status:</strong> ".$smoking;  
      $message.="<br /><strong>Date of Birth:</strong> " . $day . " / " . $month . " / " . $year;          
      $message.="<br /><strong>Occupation Description:</strong> ".$occupationdesc;           
      $message.="<br /><strong>Occupation Type:</strong> ".$occupation;
      $message.="<br /><strong>Taxable Income:</strong> ".$income;
      $message.="<br /><strong>Insurance Type Required:</strong> ";

      $message.= 'Insurance Type chosen: '.$how_many.'<br><br>';
    if ($how_many>0) {
        $message.= 'You chose the following Insurance Types:<br>';
    }
    for ($i=0; $i<$how_many; $i++) {
       $message.= ($i+1) . '- ' . $insurancetype[$i] . '<br>';
    }
        $message.= "\n\n";


      $message.="<br /><strong>Additional Information:</strong> ".$text;
      $message.="<br /><br />You will be contacted shortly to discuss your application for Business Insurance, if any details need changing please contact BUSINESS NAME by <strong>Calling PHONE</strong>";

      $temp = file_get_contents("cover.txt");
      $message = str_replace("[ALL_DETAILS]",$message,$temp);

      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $headers.= "From : BUSINESS NAME <[email protected]>\r\n";
      $headers.= "bcc :[email protected]\r\n";

      mail($to,$subject,$message,$headers);

    if(mail($to, $subject, $message)){
        echo "Your Message Has Sent";
    } 
    else{ 
        echo "there's some errors to send the mail, verify your server options";
    }

?>

I just can't get those checkboxes to display in the email that is received. It would work if JS doesn't validate, but I need the validate before submission. Pretty desperate at this point with a sore head! Help much appreciated, Cheers Mick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-12-17 04:19:51

这是由于您命名复选框的方式所致:

<input type="checkbox" name="insurancetype[]" value="Life Insurance" />

您正在像这样查找它们:

$("input[name='insurancetype']:checked")

这意味着名称属性为“insurancetype”:

<input type="checkbox" name="insurancetype" value="Life Insurance" />

It is due to the way you've named your check boxes:

<input type="checkbox" name="insurancetype[]" value="Life Insurance" />

You're looking for them like this:

$("input[name='insurancetype']:checked")

Which implies the name attribute being "insurancetype":

<input type="checkbox" name="insurancetype" value="Life Insurance" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文