javascriptphpjqueryemailphpmailer

Sending multiple checkbox values through PHPmailer


Once I hit the submit button, mail has been sent. But instead of sending selected values, it sends every value in the list. I am unable to find what am I missing. Please suggest.

As far as I have seen, I didn't get any error in it. But I think issue lies in the communicating part.

Built using

  1. HTML
  2. Javascript
  3. jQuery
  4. PHP
  5. PHPMailer

Please suggest any solution.

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();
});
  <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>

Comm.php

<?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> BUSINESS AND COMPUTING</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";
?>


Solution

  • You've gone through a very hard way.

    You could use a checkbox array like this:

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

    And using 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)
      },
    
    });
    
    });
    
    });
    

    And, in your PHP file, you could use $_POST in order to fetch the data.

    $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'];