Answer the question
In order to leave comments, you need to log in
How to extract and send text content of checkbox to mail?
Hello! There is a form with checkboxes. I need to send the text content of the selected checkboxes to the mail.
Those. value is already taken by the price value. How to send text.... for example, you have selected the "House" checkbox with the value 4000000. You need to send the word "House".
<input type="checkbox" name="chk1" value = "4000000">Дом
<input type="checkbox" name="chk1" value = "1000000">Авто
<input type="checkbox" name="chk1" value = "25000">Телефон
Answer the question
In order to leave comments, you need to log in
Well, when you render your boxes, you can specify something like this
<input type="checkbox" name="chk1" value = "4000000{//}Дом">Дом
<input type="checkbox" name="chk1" value = "1000000{//}Авто">Авто
<input type="checkbox" name="chk1" value = "25000{//Телефон}">Телефон
HTML:
...
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#ajax_form").on("submit", function(){
var data = [];
$("#ajax_form input[type='checkbox']:checked").each(function(){
data.push({value: $(this).val(), text: $("label[for="+$(this).attr("id")+"]").text()});
});
$.ajax({
type: "POST",
url: "test.php?do_action=some_action3",
data: {chk1: data},
success: function(sum){
alert("Сумма - "+sum);
}
});
return false;
});
});
</script>
</head>
...
<main>
<section>
<form method="post" action="">
<input type="hidden" name="do_action" value="some_action" />
<label for="house"><input type="checkbox" name="chk1[Дом]" id="house" value="4000000" />Дом</label>
<label for="avto"><input type="checkbox" name="chk1[Авто]" id="avto" value="1000000" />Авто</label>
<label for="phone"><input type="checkbox" name="chk1[Телефон]" id="phone" value="25000" />Телефон</label>
<button type="submit">go! go! go!</button>
</form>
</section>
<p> или например так:</p>
<section>
<form method="post" action="">
<input type="hidden" name="do_action" value="some_action2" />
<label for="house2"><input type="checkbox" name="chk1[0]" id="house2" value="4000000" />Дом</label>
<input type="hidden" name="chk1_value[0]" value="Дом" />
<label for="avto2"><input type="checkbox" name="chk1[1]" id="avto2" value="1000000" />Авто</label>
<input type="hidden" name="chk1_value[1]" value="Авто" />
<label for="phone2"><input type="checkbox" name="chk1[2]" id="phone2" value="25000" />Телефон</label>
<input type="hidden" name="chk1_value[2]" value="Телефон" />
<button type="submit">go! go! go!</button>
</form>
</section>
<p>ну или с AJAX, вот так:</p>
<section>
<form method="post" action="" id="ajax_form">
<input type="hidden" name="do_action" value="some_action3" />
<label for="house3"><input type="checkbox" name="chk1[]" id="house3" value="4000000" />Дом</label>
<label for="avto3"><input type="checkbox" name="chk1[]" id="avto3" value="1000000" />Авто</label>
<label for="phone3"><input type="checkbox" name="chk1[]" id="phone3" value="25000" />Телефон</label>
<button type="submit">go! go! go!</button>
</form>
</section>
</main>
if (isset($_REQUEST['do_action'])) {
switch ($_REQUEST['do_action']) {
case 'some_action':
foreach ($_REQUEST['chk1'] as $k => $v) {
echo $k.' - '.$v.'<br />';
}
$sum = array_sum($_REQUEST['chk1']);
echo '<br />Сумма - '.$sum;
break;
case 'some_action2':
foreach ($_REQUEST['chk1'] as $k => $v) {
echo $_REQUEST['chk1_value'][$k].' - '.$v.'<br />';
}
$sum = array_sum($_REQUEST['chk1']);
echo '<br />Сумма - '.$sum;
break;
case 'some_action3':
$sum = 0;
for ($i = 0; $i < count($_REQUEST['chk1']); $i++) {
//echo $_REQUEST['chk1'][$i]['text']; // do something with text here!
$sum += $_REQUEST['chk1'][$i]['value'];
}
echo $sum;
exit();
break;
}
}
This is exactly what the name attribute is intended for, which for some reason is the same on all three checkboxes.
The checkbox does not imply the selection of one element from several. Therefore, if there are several checkboxes in one form, then their name must be different !
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question