checkbox with ordered selection array for php - php

<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.

Related

I want to get result of each checkbox in PHP POST by ajax on real time checkbox selection

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>

How to make GET from "field=foo&field=bar" to "field=foo,bar"

I have a html form with many fields:
<form action="" method="get">
...
<input type="checkbox" name="price" id="price-1000" value="1000">
<input type="checkbox" name="price" id="price-2000" value="2000">
<input type="checkbox" name="price" id="price-3000" value="3000">
...
<input type="submit" value="send">
</form>
On check and send I have the following query string: ?price=1000&price=2000
How make it ?price=1000,2000 in browser URL
I think I need a rewrite rule via htaccess? Or is there some other way?
Using Jquery It will be possible.
index.php
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form action="redex.php" method="get">
<input type="checkbox" id="price-1000" value="1000" class="ckbox">
<input type="checkbox" id="price-2000" value="2000" class="ckbox">
<input type="checkbox" id="price-3000" value="3000" class="ckbox">
<input type="hidden" name="price" id="price-1000" value="1000" class="ckvalues">
<input type="submit" value="send">
</form>
<script type="text/javascript">
$(document).ready(function() {
var favorite = [];
$('.ckbox').change(function() {
$.each($("input[type='checkbox']:checked"), function(){
favorite.push($(this).val());
});
var str1 = favorite.toString()
$('.ckvalues').val(str1)
favorite.length = 0;
});
});
</script>
redex.php
<?php
echo $_GET['price'];
?>
May this help you.

Submit Multiple Forms with jquery add onchange event for each value to fire php file

I have some code which works fine. The jquery script submits all the data from the html forms when clicking the submit link. By clicking the submit button the forms reduce themselves to what was selected and underneath the forms the products for this filtering event show up.
I want to add an onchange function to every input value to show the result of this filtering in advance. The result should appear in the result div and should be updated live when changing the checkbox value.
How do i achieve this?
I need help to create the jquery function which collects the clicked checkboxes.
Here is the code.
http://phpfiddle.org/main/code/gc4t-grw3
PHP
<?php
$result = (isset($_POST)) ? $_POST : "";
foreach ($_POST as $key => $b) {
if (is_array($b)){
$result = array_unique($b);
$option_filter_help = implode(", ", $result);
}
}
print_r($result);
if (isset($_POST)){
echo($option_filter_help);
}
?>
Javascript
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#submit").click(function(event) {
inputs = $(".myForm input");
inputs.each(function(index) {
$(inputs[index]).clone(true).appendTo("#submit-form").css("display", "none");
});
});
});
HTML
<div id="results"></div>
<form action="" method="POST" class="myForm" onsubmit="return false;">
<label><input type="checkbox" name="optfilter[]" value="36" />36</label>
<label><input type="checkbox" name="optfilter[]" value="26" />26</label>
<label><input type="checkbox" name="optfilter[]" value="16" />16</label>
<label><input type="checkbox" name="optfilter[]" value="66" />66</label>
<label><input type="checkbox" name="optfilter[]" value="76" />76</label>
<label><input type="checkbox" name="optfilter[]" value="86" />86</label>
</form>
<form action="" method="POST" class="myForm" onsubmit="return false;">
<label><input type="checkbox" name="optfilter[]" value="136"/>136</label>
<label><input type="checkbox" name="optfilter[]" value="126"/>126</label>
<label><input type="checkbox" name="optfilter[]" value="116"/>116</label>
<label><input type="checkbox" name="optfilter[]" value="66" />66</label>
<label><input type="checkbox" name="optfilter[]" value="176"/>176</label>
<label><input type="checkbox" name="optfilter[]" value="86" />86</label>
</form>
<form action="" method="POST" class="myForm" onsubmit="return false;">
<label><input type="checkbox" name="optfilter[]" value="236"/>236</label>
<label><input type="checkbox" name="optfilter[]" value="226"/>226</label>
<label><input type="checkbox" name="optfilter[]" value="216"/>216</label>
<label><input type="checkbox" name="optfilter[]" value="26"/>26</label>
<label><input type="checkbox" name="optfilter[]" value="276"/>276</label>
<label><input type="checkbox" name="optfilter[]" value="286"/>286</label>
</form>
<!-- Target form -->
<form action="" method="POST" id="submit-form">
<input type="submit" id="submit" />
</form>
Initiate event listener for checkbox change and append result data to the container.
Example:
$('input[type="checkbox"]').change(function() {
var vals = $('input[type="checkbox"]:checked').map(function(){
return $(this).val();
}).get();
$('#results').text((vals.length ? vals.join(', ') : 'Nothing selected.'))
});

Select all checkbox with jQuery form POST for multiple checkboxes

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>

checkbox in groups?

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>

Categories