通过PHPmailer发送多个复选框值

时间:2017-11-14 11:03:12

标签: javascript php jquery email phpmailer

一旦我输入提交按钮。邮件已发送。但是,它不是发送选定的值,而是发送列表中的每个值。我无法找到我失踪的东西。请建议。

据我所知,我没有收到任何错误。但我认为问题在于沟通部分。

内置使用 1)html 2)javascript 3)jquery 4)PHP 5)PHPMailer

请建议任何解决方案。



var address = document.getElementById('faddress');
var optiona = document.getElementById('optiona');
var optionb = document.getElementById('optionb');
var optionc = document.getElementById('optionc');
var optiond = document.getElementById('optiond');
var optione = document.getElementById('optione');
var optionf = document.getElementById('optionf');
var optiong = document.getElementById('optiong');
var optionh = document.getElementById('optionh');
var optioni = document.getElementById('optioni');
var optionj = document.getElementById('optionj');
var sectiona = document.getElementById('sectiona');

function checkErrors(){
  var error_count = 0;

  if(error_count == 0){

     document.getElementById('container').innerHTML = "<div class='thank_con'><div class='thankyou'><h2>Thank You</h2><p>We will get back to you shortly.</p><a href='https://www.google.co.uk/' target='_blank'><img src='viewsite.png' class='imgcls'></a></div> </div>";
     var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {     
        if(this.responseText == "OKAY"){
          
        }
      }
      };
      xhttp.open("GET", "comm.php? address="+address.value+"&optiona="+optiona.value+"&optionb="+optionb.value+"&optionc="+optionc.value+"&optiond="+optiond.value+"&optione="+optione.value+"&optionf="+optionf.value+"&optiong="+optiong.value+"&optionh="+optionh.value+"&optioni="+optioni.value+"&optionj="+optionj.value+"&sectiona="+sectiona.value);
      xhttp.send();
  }
}


document.getElementById("submit_button").addEventListener("click", function(){
  checkErrors();
});
&#13;
  <form method="POST" name="contactform" action="contact-form-handler.php">

    
                <input class="form-check-input " type="checkbox" name="optiona" id="optiona" value="ESOL - National 3: Ayr">
               
                <input class="form-check-input " type="checkbox" name="optionb" id="optionb" value="ESOL - National 4: Kilmarnock/Kilwinning">
                
                <input class="form-check-input " type="checkbox" name="optionc" id="optionc" value="ESOL - National 5: Ayr / Kilmarnock">
                
                <input class="form-check-input " type="checkbox" name="optiond" id="optiond" value="ESOL - Higher: Ayr/Kilmarnock">
                
                <input class="form-check-input " type="checkbox" name="optione" id="optione" value="PEZ: Ayr/Kilmarnock/Kilwinning">
               
                <input class="form-check-input " type="checkbox" name="optionf" id="optionf" value="PEZ Plus: Ayr/Kilmarnock/Kilwinning /Cumnock">
                
                <input class="form-check-input " type="checkbox" name="optiong" id="optiong" value="Step Into Employment: Kilwinning">
                
                <input class="form-check-input " type="checkbox" name="optionh" id="optionh" value="Step Into Youthwork: Ayr">
                
                <input class="form-check-input " type="checkbox" name="optioni" id="optioni" value="Twenty Four/Seven Plus: Ayr/Kilmarnock/Kilwinning">
               
                <input class="form-check-input " type="checkbox" name="optionj" id="optionj" value="Princes Trust (Feb 18 start): Ayr/Kilmarnock/Kilwinning">
                
                <input class="form-check-input " type="checkbox" name="sectiona" id="sectiona" value="Introduction to Business and Computing: Kilwinning">

      <input type="text" class="textbx form-control" name="faddress" id="faddress" placeholder="Enter your email">
      <button type="submit" name="submit" class="subsc" id="submit_button"></button>
  </form>
&#13;
&#13;
&#13;

Comm.php

&#13;
&#13;
<?php 
date_default_timezone_set("America/New_York");
require 'PHPMailer/PHPMailerAutoload.php';

   class MyDB extends SQLite3 {
      function __construct() {
         $this->open('south_bay.sqlite');
      }
   }

$address = $_GET['address'];
$optiona = $_GET['optiona'];
$optionb = $_GET['optionb'];
$optionc = $_GET['optionc'];
$optiond = $_GET['optiond'];
$optione = $_GET['optione'];
$optionf = $_GET['optionf'];
$optiong = $_GET['optiong'];
$optionh = $_GET['optionh'];
$optioni = $_GET['optioni'];
$optionj = $_GET['optionj'];
$sectiona = $_GET['sectiona'];
$monthNum  = date("m");
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F'); 
$date = $monthName.date("-d-Y").' '.date("h:i:s A");

$mail = new PHPMailer();
$body = "<h1>Here are your customer details</h1>
         <table border='1'>
         <tr>
         <td><p><strong> EMAIL ADDRESS: </strong>$address</p></td>
         </tr>         
         <tr>
         <td><p><strong> PARTNERSHIP AND EMPLOYABLITY COURSES - CAMPUS LOCATION</strong></p></td>
         </tr> 
         <tr>
         <td><p>$optiona</p></td>
         </tr>
         <tr>
         <td><p>$optionb</p></td>
         </tr>
         <tr>
         <td><p>$optionc</p></td>
         </tr>
         <tr>
         <td><p>$optiond</p></td>
         </tr>
         <tr>
         <td><p>$optione</p></td>
         </tr>
         <tr>
         <td><p>$optionf</p></td>
         </tr>
         <tr>
         <td><p>$optiong</p></td>
         </tr>
         <tr>
         <td><p>$optionh</p></td>
         </tr>
         <tr>
         <td><p>$optioni</p></td>
         </tr>
         <tr>
         <td><p>$optionj</p></td>
         </tr>
         <tr>
         <td><p><strong> BUSSINESS AND COUMPUTING</strong></p></td>
         </tr>
         <tr>
         <td><p>$sectiona</p></td>  
         </tr>
        </table>";

$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;   // set the SMTP port for the GMAIL server
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username   = "testing@gmail.com";  // GMAIL username
$mail->Password   = "welcome$123";            // GMAIL password
$mail->AddAddress('testing@gmail.com');
$mail->AddAddress('testing@gmail.com');
$mail->SetFrom('testing@gmail.com');
$mail->Subject = 'Contact Form';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();

echo "OKAY";
?>
&#13;
&#13;
&#13;

一旦我输入提交按钮。邮件已发送。但是,它不是发送选定的值,而是发送列表中的每个值。我无法找到我失踪的东西。请建议。

据我所知,我没有收到任何错误。但我认为问题在于沟通部分。

内置使用 1)html 2)javascript 3)jquery 4)PHP 5)PHPMailer

请建议任何解决方案。

1 个答案:

答案 0 :(得分:1)

你经历了很艰难的一切。

你可以使用像这样的复选框数组:

  <form method="POST" id="contactform" name="contactform" action="contact-form-handler.php">


                <input class="form-check-input " type="checkbox" name="option[]" id="optiona" value="ESOL - National 3: Ayr">

                <input class="form-check-input " type="checkbox" name="option[]" id="optionb" value="ESOL - National 4: Kilmarnock/Kilwinning">

                <input class="form-check-input " type="checkbox" name="option[]" id="optionc" value="ESOL - National 5: Ayr / Kilmarnock">

                <input class="form-check-input " type="checkbox" name="option[]" id="optiond" value="ESOL - Higher: Ayr/Kilmarnock">

                <input class="form-check-input " type="checkbox" name="option[]" id="optione" value="PEZ: Ayr/Kilmarnock/Kilwinning">

                <input class="form-check-input " type="checkbox" name="option[]" id="optionf" value="PEZ Plus: Ayr/Kilmarnock/Kilwinning /Cumnock">

                <input class="form-check-input " type="checkbox" name="option[]" id="optiong" value="Step Into Employment: Kilwinning">

                <input class="form-check-input " type="checkbox" name="option[]" id="optionh" value="Step Into Youthwork: Ayr">

                <input class="form-check-input " type="checkbox" name="option[]" id="optioni" value="Twenty Four/Seven Plus: Ayr/Kilmarnock/Kilwinning">

                <input class="form-check-input " type="checkbox" name="option[]" id="optionj" value="Princes Trust (Feb 18 start): Ayr/Kilmarnock/Kilwinning">

                <input class="form-check-input " type="checkbox" name="sectiona" id="sectiona" value="Introduction to Business and Computing: Kilwinning">

      <input type="text" class="textbx form-control" name="faddress" id="faddress" placeholder="Enter your email">
      <button type="submit" name="submit" class="subsc" id="submit_button"></button>
  </form>

使用jQuery,

$(function(){

$('#contactform').submit(function(){

var data = $(this).serialize();

$.ajax({
  method: 'POST',
  url: 'path-to-your-php-file-here',
  data: data,
  success: function(result){
    console.log(result) //what to do on success
  },
  error: function(result){
    console.log(result) //what to do on error
  },
  complete: function(result){
    console.log(result) //what to do on complete (success or error)
  },

});

});

});

并且,在您的PHP文件中,您可以使用$ _POST来获取数据。

$address = $_POST['address'];
$option_array = $_POST['option'];

$option_to_string = implode(", ", $option_array); //if you want it as a comma separated string
$sectiona = $_POST['sectiona'];