For a project I want to implement a nice filtering mechanism with multiple checkboxes. I get the checkboxes to work correctly as well as a jQuery function to automatically POST the form when checking a checkbox.
Now I want to add a "select all" checkbox above the checkboxes, but I cannot seem to find the correct way. I have tried a dozen solutions for (somewhat) similar questions but I cannot get it to work correctly and consistent.
The HTML part is something like this:
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green"> Green</label><br/><br/>
<input type="checkbox" /> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand"> SomeOtherBrand</label><br/>
</form>
The jQuery part I use to post the form on each click on the checkbox (is not sufficient):
$('input:checkbox:').live('click',function() {
$(this).closest('form').submit();
});
My question now is what do I need for the jQuery part to make sure this works correctly?
I want to be able to click the label to deselect all from that group and select only that one checkbox. It also needs to POST the form for the array values. And lastly if all checkboxes are checked manually the "select all" one has to be checked as well.
Hopefully someone can help me out as I am stuck with this for a long time...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#colorall").click(function()
{
var checked_status = this.checked;
$(".color").each(function()
{
this.checked = checked_status;
});
});
$(".color").click(function(){
if($(".color").length == $(".color:checked").length) {
document.getElementById("colorall").checked = true;
} else {
$("#colorall").removeAttr("checked");
}
});
$("#brandall").click(function()
{
var checked_status = this.checked;
$(".brand").each(function()
{
this.checked = checked_status;
});
});
$(".brand").click(function(){
if($(".brand").length == $(".brand:checked").length) {
document.getElementById("brandall").checked = true;
} else {
$("#brandall").removeAttr("checked");
}
});
});
</script>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" id="colorall" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow" class="color"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue" class="color"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red" class="color"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green" class="color"> Green</label><br/><br/>
<input type="checkbox" id="brandall"/> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike" class="brand"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas" class="brand"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand" class="brand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand" class="brand"> SomeOtherBrand</label><br/>
</form>
Related
I want to get result of each checkbox in PHP POST by ajax on real time checkbox selection
function showValues() {
var str = $("form").serialize();
$("#results").text(str);
}
$("input[type='checkbox']").on("click", showValues);
showValues();
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<form>
<input type="checkbox" name="check" value="check1" id="ch1">
<label for="ch1">check1</label>
<input type="checkbox" name="check" value="check2" id="ch2">
<label for="ch2">check2</label>
<input type="checkbox" name="check" value="check3" id="ch3">
<label for="ch2">check3</label>
<input type="checkbox" name="check" value="check4" id="ch4">
<label for="ch2">check4</label>
<input type="checkbox" name="check" value="check5" id="ch5">
<label for="ch2">check5</label>
</form>
<tt id="results"></tt>
<form method='post' action=''>
<label for="x1">X1</label>
<input id="x1" name="checkbox[]" type="checkbox" value="x1" />
<label for="x2">X2</label>
<input id="x2" name="checkbox[]" type="checkbox" value="x2" />
<label for="x3">X3</label>
<input id="x3" name="checkbox[]" type="checkbox" value="x3" />
<label for="x4">X4</label>
<input id="x4" name="checkbox[]" type="checkbox" value="x4" />
<button type='submit' name='submit'>Submit</button>
</form>
In this form How can i get the selection order of the checkboxes inside the php array
Just if i submitted the form after clicking x4 > x2 > x3 > x1
i want to get ordered in the array as it is selected checkbox[x4, x2, x3, x1]
The normal array i get is as it is ordered checkbox[x1, x2, x3, x4].
Check the clicked order using jquery
push the value to array on change event.if is checked push to the array.is unchecked value remove from array
Join the array with , add the value to hidden input .you could post this value to server
var arr=[]
$('input[type=checkbox]').on('change', function() {
if ($(this).is(':checked')) {
arr.push($(this).val())
} else {
var r =arr.indexOf($(this).val())
arr.splice(r, 1)
}
$('input[type=hidden]').val(arr.join(',')) //for server use
console.log(arr)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='post' action=''>
<label for="x1">X1</label>
<input id="x1" name="checkbox[]" type="checkbox" value="x1" />
<label for="x2">X2</label>
<input id="x2" name="checkbox[]" type="checkbox" value="x2" />
<label for="x3">X3</label>
<input id="x3" name="checkbox[]" type="checkbox" value="x3" />
<label for="x4">X4</label>
<input id="x4" name="checkbox[]" type="checkbox" value="x4" />
<input type="hidden" name="clickedorder" >
<button type='submit' name='submit'>Submit</button>
</form>
Updated
change the label value depend on clicking order
var arr=[]
$('input[type=checkbox]').on('change', function() {
if ($(this).is(':checked')) {
arr.push($(this).val())
} else {
var r =arr.indexOf($(this).val())
arr.splice(r, 1)
}
$('input[type=checkbox]').each(function(a){
//console.log(arr.indexOf($(this).val()))
if(arr.indexOf($(this).val())>-1){
$(this).prev('label').text($(this).val().toUpperCase()+'-'+(arr.indexOf($(this).val())+1))
}else{
$(this).prev('label').text($(this).val().toUpperCase())
}
})
$('input[type=hidden]').val(arr.join(',')) //for server use
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='post' action=''>
<label for="x1">X1</label>
<input id="x1" name="checkbox[]" type="checkbox" value="x1" />
<label for="x2">X2</label>
<input id="x2" name="checkbox[]" type="checkbox" value="x2" />
<label for="x3">X3</label>
<input id="x3" name="checkbox[]" type="checkbox" value="x3" />
<label for="x4">X4</label>
<input id="x4" name="checkbox[]" type="checkbox" value="x4" />
<input type="hidden" name="clickedorder" >
<button type='submit' name='submit'>Submit</button>
</form>
You need to write a jquery function like this:
Add a common class to all checkboxes like class="ckbox"
then,
$(document).ready(function(){
var selected_boxes = [];
$('.ckbox').change(function() {
if($(this).is(':checked')) {
selected_boxes.push($(this).val());
$(this).prev().attr('for',$(this).val());
}
else{ // When uncheck the checkbox.
selected_boxes.splice( $.inArray($(this).val(), selected_boxes), 1 );
}
});
});
Then post this JS variable to your php code. It will give all the values in sequence in which you had selected.
What should I type in PHP instead of I got now to receive in mail all of the answers typed in checkboxes by user. Now I have only one. Thanks
PHP:
$email_message .= "Sport: ".clean_string($_POST["sport"])."\n";
$email_message .= "Music: ".clean_string($_POST["music"])."\n";
HTML:
<div id="Oobj58" class="Oobj">
<input type="checkbox" name="sport" value="Kosz" />koszykówka<br>
<input type="checkbox" name="sport" value="Zimowe" />sporty zimowe<br>
<input type="checkbox" name="sport" value="Konie" />jeździectwo konne<br>
</div>
<div id="Oobj61" class="Oobj">
<input type="checkbox" name="rozrywka" value="festiwal" />festiwale<br />
<input type="checkbox" name="rozrywka" value="wesole" />wesołe miasteczka<br />
<input type="checkbox" name="rozrywka" value="zespolowo"/>paintball,bilard,kręgle..<br/>
</div>
Edit:
Thanks but not exactly can type different 'names' cause I also have one checkboxe to set all on check at once and it works with JS like this:
<div id="Oobj61" class="Oobj">
<script language="JavaScript">
function toggle2(source) {
checkboxes = document.getElementsByName('rozrywka');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<input type="checkbox" onClick="toggle2(this)" /><br>
<input type="checkbox" name="rozrywka" value="club" />kluby<br />
<input type="checkbox" name="rozrywka" value="puby" />puby<br />
<input type="checkbox" name="rozrywka" value="koncert" />koncerty<br />
<input type="checkbox" name="rozrywka" value="festiwal" />festiwale<br />
<input type="checkbox" name="rozrywka" value="wesole" />wesołe miasteczka<br />
<input type="checkbox" name="rozrywka" value="zespolowo" />paintball,bilard,kręgle...<br
/>
</div>
I'm not 100% sure what it is you're asking here, but if you'd like to see if a checkbox is set you can do the following:
if(isset($_POST['checkbox-name-here'])){
//it's set code here, maybe set a variable...
$var = "checkbox value";}
That's a rough example, but you should get the drift.
If you're trying to get multiples - then give each checkbox a unique name:
<div id="Oobj58" class="Oobj">
<input type="checkbox" name="sport1" value="Kosz" />koszykówka<br>
<input type="checkbox" name="sport2" value="Zimowe" />sporty zimowe<br>
<input type="checkbox" name="sport3" value="Konie" />jeździectwo konne<br>
</div>
Then in PHP you can check if each value is set:
if(isset($_POST['sport1'])){$sport1="sport1";}
if(isset($_POST['sport2'])){$sport2="sport2";}
if(isset($_POST['sport3'])){$sport3="sport3";}
$email_message .= "Sports: ".clean_string($_POST["sport1"])."\n"
.clean_string($_POST["sport2"])."\n"
.clean_string($_POST["sport3"])."\n";
There are much tidier ways of doing it, but that should help you work it out?
Alternatively you could give each of the checkboxes the same name, and then construct an array out of them. This array can then be looped in the PHP email handler. Look at this SO answer for a great example: https://stackoverflow.com/a/4516887/3112128
I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>
i need to make "groups" or something with my checkboxes. i want to click on a checkbox and only those checkboxes are active which available then. Like in the car configuration. If you buy the electric mirror you cant chose the manual one.In my case i have Product 1A 1B 1C and 2A 2B 2C and if i chose product one i can only choose product 1A,1B,1C.
<input type="checkbox" value="">1A</label>
<input type="checkbox" value="">1B</label>
<input type="checkbox" value="">1C</label>
<input type="checkbox" value="">2A</label>
<input type="checkbox" value="">2B</label>
<input type="checkbox" value="">2C</label>
HTML
<input type="checkbox" class='product' value=""/>1A
<input type="checkbox" class='product' value=""/>1B
<input type="checkbox" class='product' value=""/>1C
<input type="checkbox" class='product' value=""/>2A
<input type="checkbox" class='product' value=""/>2B
<input type="checkbox"class='product' value=""/>2C
jQuery
$(function(){
var $product = $('input.product');
$product.click(function() {
$product.filter(':checked').not(this).removeAttr('checked');
});
})
FIDDLE
Added after comment from OP
This is the code after the comment that if i chose 1A, i can also click on 1B and 1C but not on 2A,2B,2C*
HTML
<input type="checkbox" class='one' value=""/>1A
<input type="checkbox" class='one' value=""/>1B
<input type="checkbox" class='one' value=""/>1C<br/>
<input type="checkbox" class='two' value=""/>2A
<input type="checkbox" class='two' value=""/>2B
<input type="checkbox" class='two' value=""/>2C
jQuery
$(function(){
var $one= $('input.one');
$one.click(function() {
$one.filter(':checked').not(this).removeAttr('checked');
if($one.filter(':checked').length>0) {
$two.attr("disabled","disabled");
}
else {
$two.removeAttr("disabled");
}
});
var $two= $('input.two');
$two.click(function() {
$two.filter(':checked').not(this).removeAttr('checked');
if($two.filter(':checked').length>0) {
$one.attr("disabled","disabled");
}
else {
$one.removeAttr("disabled");
}
});
});
Fiddle
I think you may want to look at radio button types rather
If you are using checkboxes, you should address them as an array in your PHP code as well as giving them a name attribute (which is how PHP sees them):
<input type="checkbox" name ="checkbox1" value="">1A</label>
In your PHP code:
$checkArray=$_REQUEST['checkbox1'];
foreach($checkArray as $val)
{
echo $val;// Value is echo'ed.
}
html
<input type="checkbox" class='group1' value=""/>1A
<input type="checkbox" class='group1' value=""/>1B
<input type="checkbox" class='group1' value=""/>1C
<input type="checkbox" class='group2' value=""/>2A
<input type="checkbox" class='group2' value=""/>2B
<input type="checkbox" class='group2' value=""/>2C
<br/>
jquery
<br />
<script>
$(function(){
var $product=$('input:checkbox');
$product.click(function() {
$product.attr('checked',false);
var classname=$(this).attr('class');
$('.'+classname).attr('checked','checked');
});
});
</script>