Welcome Guest, Not a member yet? Register   Sign In
Not Selected Checkbox variable -- form validation problem with checkbox value
#1

[eluser]onecode[/eluser]
Today i'm learning new class from CI --> Form (validation and helper too). I'm not found any problem, until i try for using a checkbox....and form validation. Ok if my checkbox has a value (checked), then no problem with that, but how if not checked? of course there are no post variable (if not checked), and my problem is i cannot using form validation to validate my checkbox post variable, this is my code using form validation :

Code:
//checkbox variable name --> id
//this script will give a NOTICE Error Message if there are nothing checkbox selected

$this->load->library('form_validation');
$this->form_validation->set_rules('id[]','IDContent','required');
                
    if($this->form_validation->run() == FALSE) {
                    
       $this->_load_content('edit','Nothing selected'); //my private function
                    
    }

if there are no checkbox selected, it means no post variable, if there are no post variable, then CI will be give a NOTICE error message...Before im using this validation, i think this CI function can detect if there are nothing checkbox selected, but im wrong. So this is my code :red: :

Code:
//if there are no checkbox selected then will print "Nothing selected"
if(isset($_POST['id'])) {
                
     for($i=0; $i < count($_POST['id']); $i++) {
                        
            $id    = $_POST['id'];
        $query    = "delete from content where idcontent='$id[$i]'";
        $sql    = $this->db->query($query); //deleting data from database
                                                
     }
                    
     $this->_load_content('edit','Your data has been deleted.'); // my private function
                    
}elseif(!in_array('id',$_POST)) {  

//i'm using in_array() function to detect checkbox post variable
//if checkbox not selected will print "Nothing selected"
                        
       $this->_load_content('edit','Nothing selected'); // my private function
                      
}

Can i just use CI Form Validation to solve this problem? :down: I know this problem can handled correctly just with my code, but i want to learn more from CI "how to resolve this problem if there are nothing checkbox selected using Form Validation"?

I know my english language is bad, i'm sorry for that Tongue ..If this thread is repost, i'm sorry again, but after searching in this forum, i cannot find any usefull information that can help me.. :down:
#2

[eluser]obiron2[/eluser]
You can use
Code:
if (! isset ($_POST['my_checkbox'])) {  }

To see if it exists. This obviously means that you need to explicity know the name of the checkbox.

I prefer to build a model of the form fields, use these to generate the form and then use the model to compare $_POST and see which fields are missing.

Obiron




Theme © iAndrew 2016 - Forum software by © MyBB