【问题标题】:sending multiple checkbox values through PHPmailer通过 PHPmailer 发送多个复选框值
【发布时间】:2018-04-27 07:13:00
【问题描述】:

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

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

使用 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();
});
  <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> 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";
?>

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

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

使用 1) html 2) javascript 3) jQuery 4)PHP 5) PHPMailer

请提出任何解决方案。

【问题讨论】:

    标签: javascript php jquery email phpmailer


    【解决方案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'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多