Welcome Guest, Not a member yet? Register   Sign In
[solved] check boxes
#1

[eluser]Todlerone[/eluser]
Hello all and ty for ant help. I have a form with three check boxes:
Code:
<label for="ReferralImaging">Referral Imaging:</label>
                        &lt;input type="checkbox" name="ReferralImaging[]" value="ct"&lt;?php echo set_checkbox('ReferralImaging', 'ct'); ?&gt;/&gt;CT
                        &lt;input type="checkbox" name="ReferralImaging[]" value="mri"&lt;?php echo set_checkbox('ReferralImaging', 'mri'); ?&gt;/&gt;MRI
                        &lt;input type="checkbox" name="ReferralImaging[]" value="mrisrs"&lt;?php echo set_checkbox('ReferralImaging', 'mrisrs'); ?&gt;/&gt;MRISRS
I just can't work out the details to get them to post to my database. The user guild doesn't really help.

TY
#2

[eluser]Sean Gates[/eluser]
Can you post the portion of your controller and model that are handling the form data? Also, what does the POST data look like when you get it back from the form submission? Are the checkboxes coming through in the array as you expect?
#3

[eluser]Todlerone[/eluser]
TY for your reply Sean. I'm familiar with OO PHP and new to CI. I've used forms many times...just never check boxes. Didn't expect to have a hard time with it. I feel so dumb. Here is my controller. I have removed some of the code that wasn't relevent.
Code:
function index(){
        $ReferralImaging =array();
        $this->load->model('People');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('ReferralImaging', '');
                    
        if ($this->form_validation->run() == FALSE){
            $this->load->view('header');
            $this->load->view('admin/add_person_form');
            $this->load->view('sidebars/sidebar_person_add');
            $this->load->view('footer');
        }else{
            $this->People->add_person();
            $this->load->view('header');
            $this->load->view('admin/person_add_success');
            $this->load->view('sidebars/sidebar_person_add');
            $this->load->view('footer');
        }// end form_validation
          }// end index

And here is my model.

Code:
function add_person(){
           $data = array(
            'NameL' => $this->input->post('NameL'),
            'NameF' => $this->input->post('NameF'),
            'Gender' => $this->input->post('Gender'),
            'ReferralImaging' => $this->input->post('ReferralImaging'));
        
              $this->db->insert('demographics', $data);
        }

TY
#4

[eluser]Sean Gates[/eluser]
So, the checkboxes are an array, which means the database table field should be set up to handle the array. What is the current data type for the table field you're trying to put the checkbox data into?
#5

[eluser]Todlerone[/eluser]
TY again. I have it as a char(15). There are three check boxes that give the values of ct, mri or mrisrs. Do I have to serialize() on my own or is this a built in CI function for check boxes?
#6

[eluser]smilie[/eluser]
I think (not sure tho') that you will have to serialize yourself.
However, I would suggest you to split that DB table into three fields (one for each checkbox).

I it easier to handle it that way (from code).

Let's say, in future - you only wish to get appointments for CT.
Then, you would (in PHP) have to get that serialized value out of 1 table field, unserialize it and then do preg_search on it to see if it matches your request.

If you have separate fields for each checkbox, you can simply do:

select ... from ... where ct = 1;

Cheers,
Smilie
#7

[eluser]Todlerone[/eluser]
TY smilie. I was leaning toward that, was just hoping it would be easy with the one field.

TY
#8

[eluser]Todlerone[/eluser]
Ok I decided to split them up. But I still can't get them to be selected individually (if they were on the first submit if a field doesn't pass validation.


Code:
<label for="jcc_demo_ReferralImaging">Referral Imaging:</label>
                        &lt;input type="checkbox" name="jcc_demo_Referral_ct" value="1" &lt;?php echo set_checkbox('jcc_Referral_ct', '1'); ?&gt; /&gt;CT
                        &lt;input type="checkbox" name="jcc_demo_Referral_mri" value="1" &lt;?php echo set_checkbox('jcc_demo_Referral_mri', '1'); ?&gt; /&gt;MRI
                        &lt;input type="checkbox" name="jcc_demo_Referral_mrisrs" value="1" &lt;?php echo set_checkbox('jcc_demo_Referral_mrisrs', '1'); ?&gt; /&gt;MRI-SRS

The reposted form shows up with the original check boxes unselected.
Any suggestions...TY very much
#9

[eluser]Sean Gates[/eluser]
Your form validation has:
Code:
$this->form_validation->set_rules('ReferralImaging', '');

I believe it should be:
Code:
$this->form_validation->set_rules('jcc_demo_Referral_ct', 'Referral CT', 'required');
$this->form_validation->set_rules('jcc_demo_Referral_mri', 'Referral MRI', 'required');
$this->form_validation->set_rules('jcc_demo_Referral_mrisrs', 'Referral MRISRS', 'required');

... or something like that. Basically, you were missing the validation rule (e.g. required) that you were testing against as well as a rule for each checkbox.

Try that and post success/failure.

Thanks!

EDIT: fixed the syntax on the set_rules method.
#10

[eluser]Todlerone[/eluser]
That was it Sean. TY TY TY very much.

CHEERS




Theme © iAndrew 2016 - Forum software by © MyBB