Welcome Guest, Not a member yet? Register   Sign In
forms check box question
#1

[eluser]k2zs[/eluser]
I have finished a small back end for my new codeigniter site and came across a problem submitting a form with a check box value. Since check boxes only exist in the _POST array if they are in fact checked I found I couldn't toggle a database field from 1 to 0 with a check box. As a work around I added a hidden field with the same name at the top of the form with a value of 0. Since that field name becomes an array and the db->update takes the last value of that array the field becomes 1 when the check box is checked and 0 (the value of the hidden field) when the check box isn't checked.

Is there a better way of handling this. I could see that becoming quite tedious if there were many check boxes. I had thought of trying to use the validation helper to generate "exists" and "not exists" logic and embedding an additional update call from within the output paths.

Any thoughts?
#2

[eluser]bretticus[/eluser]
[quote author="k2zs" date="1289290949"]
Any thoughts?[/quote]

Yes, however convenient inserting the $_POST variable directly into $this->db->insert() is, it's probably a bad idea. For one, any remote post attempt has the power to build queries that will break your code (with escaping on automatically, you're probably safe from injection but you never know.) The main reason, however, is you forfeit the models ability to ensure that data is formatted in the manner it was intended to be.

I recommend having model method parameters that have a one-to-one mapping with a table field.

From there it's easy to write some logic to handle check boxes.

Controller:

Code:
$this->your_model->insert(/.../, $this->input->post('check_box'));

your_model:

Code:
function insert(/.../, $check_box) {
  if ( $check_box === FALSE )
    $data['check_box'] = 0;
  else
    $data['check_box'] = 1;
  /.../
  $this->db->insert('table', $data);
}
#3

[eluser]tonanbarbarian[/eluser]
Yes / No radio buttons work a treat
Ok so some people do not like the look of them but they always return a value
The other option is to check the data before insert or update and if there is no value for the checkbox field supplied then create a 0 value
#4

[eluser]k2zs[/eluser]
[quote author="bretticus" date="1289296985"][quote author="k2zs" date="1289290949"]
Any thoughts?[/quote]

Yes, however convenient inserting the $_POST variable directly into $this->db->insert() is, it's probably a bad idea. For one, any remote post attempt has the power to build queries that will break your code (with escaping on automatically, you're probably safe from injection but you never know.) The main reason, however, is you forfeit the models ability to ensure that data is formatted in the manner it was intended to be.

I recommend having model method parameters that have a one-to-one mapping with a table field.

From there it's easy to write some logic to handle check boxes.

Controller:

Code:
$this->your_model->insert(/.../, $this->input->post('check_box'));

your_model:

Code:
function insert(/.../, $check_box) {
  if ( $check_box === FALSE )
    $data['check_box'] = 0;
  else
    $data['check_box'] = 1;
  /.../
  $this->db->insert('table', $data);
}
[/quote]



whats the "/.../" used for?
#5

[eluser]tonanbarbarian[/eluser]
you replace the /.../ with the name of your table i.e. 'users'
#6

[eluser]k2zs[/eluser]
thats what I thought but the "/.../" in the else statement through me




Theme © iAndrew 2016 - Forum software by © MyBB