CodeIgniter Forums
check-box conditioning - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: check-box conditioning (/showthread.php?tid=1002)



check-box conditioning - jaysondotp - 02-04-2015

Hi coders,


i have list of check-box and there are same value and there are not. now i want to create condition, example if i check 3 check-box with different value it retrurn false, while if i check 3 checkbox with the same value it wil return true.


how do i in php? 


any help well appreciate . . . .


thanks


RE: check-box conditioning - egall8 - 02-05-2015

(02-04-2015, 11:52 PM)jaysondotp Wrote: Hi coders,


i have list of check-box and there are same value and there are not. now i want to create condition, example if i check 3 check-box with different value it retrurn false, while if i check 3 checkbox with the same value it wil return true.


how do i in php? 


any help well appreciate . . . .


thanks

First you will have to post it somewhere so for example purposes we'll say the form/checkboxes is located at http://example.com/admin/form

We will say you are submitting the form to : http://example.com/admin/save
In the admin controllers --> save method you will have to get the data from the $_POST once you have that is when you can check if you have three values of the same like so:

PHP Code:
$i 0;
$vals =array();
foreach (
$data as $key => $value) {

if (!
in_array($value$vals)) {
$vals[] = $value;
}
else {
$i++;
}
}

if (
$i >= 3) {
return 
FALSE;
}
else {
return 
TRUE;


Or you could use jQuery to not even let it get to that point.

Code:
<script type="text/javascript">
$('#my_form').submit(function(e) {
e.preventDefault();

$('input['type="checkbox"]').each(function() {
// check each input here
});

});

</script>



RE: check-box conditioning - jaysondotp - 02-05-2015

Thanks "egall8"..

and here is the scenario. take a look an image below as you reference. check-box value is PR No. which is, when i check different PR No. it will come false. else return true. how could i make it in php or in javascript....


[Image: Untitled_3.jpg]