For example, there are checkboxes with several options in the form:

<form> <input type="checkbox" name="ch[]" id="ch-1" value="val-1"> <input type="checkbox" name="ch[]" id="ch-2" value="val-2"> <input type="checkbox" name="ch[]" id="ch-3" value="val-3"> </form> 

In js, the usual form processing (not the essence):

 $.ajax({ url: 'contacts.php', type: 'post', data: str }).done(function(msg) { .... } 

And there are contacts.php :

 <?php define("CONTACT_FORM", 'test@mail.ru'); $subject = 'Заявка'; $ch = stripslashes($_POST['ch']); $sel = stripslashes($_POST['sel']); if(!empty($_POST['ch'])) { foreach($_POST['ch'] as $check) { $ch += $check; } } $message = ' <html> <head> <title>Заявка</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> </head> <body> <p>Значения чекбоксов : '.$ch.'</p> <p>Значение селекта : '.$sel.'</p> </body> </html>'; $mail = mail(CONTACT_FORM, $subject, $message, "MIME-Version: 1.0\r\n" ."From: ".$name." <".CONTACT_FORM.">\r\n" ."Reply-To: ".$email."\r\n" ."Content-type: text/html; charset=UTF-8\r\n"); if($mail){ echo "OK"; } ?> 

Either only one checkbox value comes to the mail, or 0 .

Question: what am I doing wrong in the .php file. How can I fix that all the checkbox values ​​come to the mail?

  • You need to go through the checkbox values ​​through forech - Evgeniy Mikhalichenko
  • I do not understand php at all, how to do this is not clear! - HamSter
  • Right now, wait a minute - Evgeniy Mikhalhenko
  • if(!empty($_POST['ch'])) { foreach($_POST['ch'] as $check) { echo $check; } } if(!empty($_POST['ch'])) { foreach($_POST['ch'] as $check) { echo $check; } } How then to insert all values ​​into $message problem? - HamSter
  • @ElenaSemenchenko, name="ch" -> name="ch[]" somehow seems so. - Visman

3 answers 3

Found the reason:

Non working code: $ch += $check;

Work code: $ch .= $check;

Happened:

.html:

 <form> <input type="checkbox" name="ch[]" id="ch-1" value="val-1"> <input type="checkbox" name="ch[]" id="ch-2" value="val-2"> <input type="checkbox" name="ch[]" id="ch-3" value="val-3"> </form> 

.php:

 <?php define("CONTACT_FORM", 'test@mail.ru'); $subject = 'Заявка'; $ch = stripslashes($_POST['ch']); $sel = stripslashes($_POST['sel']); if(!empty($_POST['ch'])) { foreach($_POST['ch'] as $check) { $ch .= $check; } } $message = ' <html> <head> <title>Заявка</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> </head> <body> <p>Значения чекбоксов : '.$ch.'</p> <p>Значение селекта : '.$sel.'</p> </body> </html>'; $mail = mail(CONTACT_FORM, $subject, $message, "MIME-Version: 1.0\r\n" ."From: ".$name." <".CONTACT_FORM.">\r\n" ."Reply-To: ".$email."\r\n" ."Content-type: text/html; charset=UTF-8\r\n"); if($mail){ echo "OK"; } ?> 

    You need to get all the selected checkbox , and get the value of select and send a request to contacts.php AJAX, passing the values ​​of checkbox and select to data

    Here is an example:

     $(function(){ var check = function(){ var cBox = []; // в цикле собираем значения всех установленных чекбоксов и записываем их в массив cBox $("input[type=checkbox]:checked").each(function(){ cBox.push($(this).attr("value")); }); // получаем значение выбранного select var selectedValue = $("option:selected").attr("value"); //debug alert("CheckBox [0]: " + cBox[0]); alert("CheckBox [1]: " + cBox[1]); alert("CheckBox [2]: " + cBox[2]); alert("Select: " + selectedValue); //debug $.ajax({ url: 'contacts.php', type: 'post', // передаем в data массив со значениями checkbox и значение select data: { ch:cBox, sel:selectedValue } }); } $("#submit").on("click", check); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" name="ch" id="ch-1" value="val-1"> <input type="checkbox" name="ch" id="ch-2" value="val-2"> <input type="checkbox" name="ch" id="ch-3" value="val-3"> <select name="sel" > <option value="val-1" selected>val1</option> <option value="val-2">val2</option> </select> <input type="button" id="submit" value="Submit" /> </form> 

    • And through php for ? This option is interesting, but it does not quite fit me! - HamSter
    • $ checkBoxValues ​​= ""; foreach ($ c as $ ch) {$ checkBoxValues ​​+ = $ c + ""; } and already in the HTML email, pass not $ ch but $ checkBoxValues - tCode
    • why is 0 transmitted ( - HamSter

    That's exactly how it works, screenshot

    enter image description here

    contacts.php

     <?php if (isset($_REQUEST['ch'])) { print_r($_REQUEST['ch']); } 

    html

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" name="ch" id="ch-1" value="val-1"> <input type="checkbox" name="ch" id="ch-2" value="val-2"> <input type="checkbox" name="ch" id="ch-3" value="val-3"> <select name="sel"> <option value="val-1" selected>val1</option> <option value="val-2">val2</option> </select> <input type="button" id="submit" value="Submit" /> </form> <div id="result"></div> <script> $(function() { var check = function() { var cBox = []; // в цикле собираем значения всех установленных чекбоксов и записываем их в массив cBox $("input[type=checkbox]:checked").each(function() { cBox.push($(this).attr("value")); }); // получаем значение выбранного select var selectedValue = $("option:selected").attr("value"); $.ajax({ url: 'contacts.php', type: 'post', // передаем в data массив со значениями checkbox и значение select data: { ch: cBox, sel: selectedValue } }).done(function(msg) { $('#result').html(msg); }); } $("#submit").on("click", check); }); </script>