简体   繁体   中英

save checkbox in php but returned incorrect

Maybe my question is familiar in this forum and my code more or less similar with many answer in here

i have a table with the data like this :

 table, th, td { border: 1px solid #aaa; border-collapse: collapse; padding: 3px; } 
 <form name="test_form" method="post" action="<?= base_url().'save_time'; ?>"> <input type="submit" name="btn-submit" value="Save"/> <table> <thead> <tr> <th>No</th> <th><input type="checkbox" id="checkall"/></th> <th>Day<br></th> <th>Opening Time</th> <th>Closing Time</th> <th>Full Day<br></th> </tr> </thead> <tbody> <tr> <td>1</td> <td><input type="checkbox" name="cekday[]" value="1"/></td> <td>Monday</td> <td><input type="text" name="open_time[]" value="" readonly="true"/></td> <td><input type="text" name="close_time[]" value="" readonly="true"/></td> <td><input type="checkbox" name="full_day[]" value="0"/></td> </tr> <tr> <td>2</td> <td><input type="checkbox" name="cekday[]" value="2" checked="checked"/></td> <td>Tuesday</td> <td><input type="text" name="open_time[]" value="" readonly="true"/></td> <td><input type="text" name="close_time[]" value="" readonly="true"/></td> <td><input type="checkbox" name="full_day[]" value="1" checked="checked"/></td> </tr> <tr> <td>3</td> <td><input type="checkbox" name="cekday[]" value="3" checked="checked"/></td> <td>Wednesday</td> <td><input type="text" name="open_time[]" value="" readonly="true"/></td> <td><input type="text" name="close_time[]" value="" readonly="true"/></td> <td><input type="checkbox" name="full_day[]" value="1" checked="checked"/></td> </tr> <tr> <td>4</td><td><input type="checkbox" name="cekday[]" value="4" checked="checked"/></td> <td>Thursday</td> <td><input type="text" name="open_time[]" value="" readonly="true"/></td> <td><input type="text" name="close_time[]" value="" readonly="true"/></td> <td><input type="checkbox" name="full_day[]" value="1" checked="checked"/></td> </tr> <tr> <td>5</td> <td><input type="checkbox" name="cekday[]" value="5" checked="checked"/></td> <td>Friday</td> <td><input type="text" name="open_time[]" value="" readonly="true"/></td> <td><input type="text" name="close_time[]" value="" readonly="true"/></td> <td><input type="checkbox" name="full_day[]" value="1" checked="checked"/></td> </tr> <tr> <td>6</td> <td><input type="checkbox" name="cekday[]" value="6" checked="checked"/></td> <td>Saturday</td> <td><input type="text" name="open_time[]" value="" readonly="true"/></td> <td><input type="text" name="close_time[]" value="" readonly="true"/></td> <td><input type="checkbox" name="full_day[]" value="1" checked="checked"/></td> </tr> <tr> <td>7</td> <td><input type="checkbox" name="cekday[]" value="7" checked="checked"/></td> <td>Sunday</td> <td><input type="text" name="open_time[]" value="03:00:00"/></td> <td><input type="text" name="close_time[]" value="22:00:00"/></td> <td><input type="checkbox" name="full_day[]" value="0"/></td> </tr> </tbody> </table> </form> 

or in jsfiddle

then i want to save data with condition :

  1. if cekday is clicked then opening_time and close_time must filled
  2. if cekday is clicked and full_day is clicked too then opening_time and close_time fill with null value

my controller :

public function save_time() {
    $detail = array();
    $numcheck = count($this->input->post('cekday'));

    for ($i = 0; $i < $numcheck; $i++) {
        $day = $this->input->post('cekday');

        if(isset($day[$i])) {
            $open_time = $this->input->post('opening_time');
            $close_time = $this->input->post('closing_time');
            $full = $this->input->post('full_day');

            $detail[] = array(
                        'day'           => $day[$i],
                        'opening_time'  => isset($full[$i]) ? $open_time[$i] : null,
                        'closing_time'  => isset($full[$i]) ? $close_time[$i] : null,
                        'full_day'      => isset($full[$i]) ? '1' : '0'
                    );  
        }   
    }
}

var_dump($detail);

but when i checked the result is not accordance as my expectation :

Array 
(
[day] => Array
    (
        [0] => 2
        [1] => 3
        [2] => 4
        [3] => 5
        [4] => 6
        [5] => 7
    )

[opening_time] => Array
    (
        [0] => 
        [1] => 3:00
        [2] => 3:00
        [3] => 3:00
        [4] => 3:00
        [5] => 3:00
        [6] => 3:00
    )

[closing_time] => Array
    (
        [0] => 
        [1] => 
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 22:00
    )

[full_day] => Array
    (
        [0] => 1
        [1] => 1
        [2] => 1
        [3] => 1
        [4] => 1
    )

)

finally i want to save data using insert_batch, thanks for your help.

To get the above mentioned output, you have to do the below changes in code.

1) First you have to specify id's for all your html input elements to validate these before save.

2)On form submit you have to check for all the form validations. onnsubmit="return savevalidate()" --- append this in form tag.

//javascript function
    function savevalidate(){
        if($('#cekday1').is(":checked") == true){
        // do your other validations
    }
    else 
    return false;
    }

similarly other validations too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM