CodeIgniter Forums
How to create IF->ELSE with input->post() with checkbox array? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: How to create IF->ELSE with input->post() with checkbox array? (/showthread.php?tid=66222)



How to create IF->ELSE with input->post() with checkbox array? - rubens - 09-22-2016

Hi guys.

I have a form that has several checkbox with the same "name" attribute. I'm trying to do a conditional to know if any checkbox is checked or not, but I can not make it work. See how it is:


PHP Code:
// View (have foreach checkboxes with the name 'service[]', no other input/select/etc )
<input type "checkbox" name "service[]" value "$numeric_id">

// Controller
$item $this->input->post ('service'true);
if(
$item){ //Run only when submit the form
 
 if ($item == NULL) {
 
   // function if no checkbox is checked
 
   // return to the same page with redirect(current_url());
 
 }else{
 
  // function if at least one checkbox was marked
 
  // return to the same page with redirect(current_url());
 
 }
}else{
 
 //show the view with the checkboxes and submit form button


The problem is that when i don't select any checkbox the input->post("service") is not NULL, not as empty or equals "" (empty).


PHP Code:
if($item==NULLnot work
if($item==FALSEnot work
if($item=='') or if($item==' 'not work
if(empty($item)) not work
if(!isset($item)) not work
if(!is_array($item)) or (is_array($item)) not work
foreach($item as $key) or foreach($item as $key => $valuenot work 


Any idea what's wrong?


RE: How to create IF-&gt;ELSE with input-&gt;post() with checkbox array? - Wouter60 - 09-23-2016

PHP Code:
if($item){ //Run only when submit the form
 
 if ($item == NULL) { 

This will never work. If you haven't selected any checkbox, $item will be empty, so the line with "if ($item == NULL) {" will not be executed.
I assume that your form has a submit button, probably with the name "submit".
PHP Code:
$submit $this->input->post('submit');
if (
$submit) {
 
 $items $this->input->post('service');
 
 if (! empty($items)) {
 
   foreach($items as $item) {
 
       //process the checkboxes that where checked
 
   }
 
 }
 
 else {
 
     // redirect
 
 }





RE: How to create IF-&gt;ELSE with input-&gt;post() with checkbox array? - rubens - 09-23-2016

(09-23-2016, 03:51 AM)Wouter60 Wrote:
PHP Code:
if($item){ //Run only when submit the form
 
 if ($item == NULL) { 

This will never work. If you haven't selected any checkbox, $item will be empty, so the line with "if ($item == NULL) {" will not be executed.
I assume that your form has a submit button, probably with the name "submit".
PHP Code:
$submit $this->input->post('submit');
if (
$submit) {
 
 $items $this->input->post('service');
 
 if (! empty($items)) {
 
   foreach($items as $item) {
 
       //process the checkboxes that where checked
 
   }
 
 }
 
 else {
 
     // redirect
 
 }



Thanks man, this work fine to me. Big Grin