Pass the values of the form's checkboxes to php
For example in the form there are checkboxes with several options:
<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 point):
$.ajax({
url: 'contacts.php',
type: 'post',
data: str
}).done(function(msg) { .... }
And there is contacts.php:
<?php
define("CONTACT_FORM", '[email protected]');
$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 is sent to the mail, or 0
.
Question: what am I doing wrong in the. php file. How can I fix it so that all the values of the checkbox are sent to the mail?
1
3 answers
Found the reason:
Non-working code: $ch += $check;
Working code: $ch .= $check;
Turn out:
.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", '[email protected]');
$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";
}
?>
1
Author: HamSter, 2016-11-10 13:49:44
You need to get all the selected checkbox
, and get the value select
and send an AJAX request to contacts.php
, passing the values checkbox
and select
{[9 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>
0
Author: tCode, 2016-11-10 11:20:11
That's exactly how it works, screenshot
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>
0
Author: Jean-Claude, 2016-11-10 12:24:34