Welcome Guest, Not a member yet? Register   Sign In
Checkbox Values
#1

[eluser]CI_Newb[/eluser]
I have a few checkboxes in my application. I want them as basically Y/N checkboxes but when checked, Y is submitted, unchecked 0 is submitted.

Code:
<input type="checkbox" name="oe_related" id="oe_related" value="Y">

Not sure how to get N to submit if the checkbox is not checked.

Anyone?

EDIT: forum seemed to have stripped out the set_checkbox part, but its there.
#2

[eluser]sketchynix[/eluser]
An html checkbox will not be submitted unless it is checked. You could use javascript to submit the form and have it submit unchecked checkboxes as null, but the simpler way is to use isset in php to see if the checkbox was submitted.
#3

[eluser]CI_Newb[/eluser]
So if an html checkbox isn't submitted if unchecked, wouldn't it use the mysql default value for that field?

I have the default value for them set to "N" yet it still returns "0" unless checked.
#4

[eluser]sketchynix[/eluser]
There is a layer (PHP) in-between the html and mysql which would be responsible for the value stored in the database and retrieved from the database. Could you post the relevant model, controller, and view in question?
#5

[eluser]CI_Newb[/eluser]
only including checkboxes since form is rather huge

model
Code:
function add_incident()
    {
        $new_insert_data = array(
            'oe_related' => $this->input->post('oe_related'),    
            'oe_stuckOrder' => $this->input->post('oe_stuckOrder'),    
            'np_failure' => $this->input->post('np_failure')
            );
        
        $insert = $this->db->insert('NS_data', $new_insert_data);
        return $insert;
    }

controller
Code:
function add_incident()
    {
        $this->load->library('form_validation');
        $data['userInfo'] = $this->user_model->find_user();
        
        $this->form_validation->set_rules('oe_related', '');
        $this->form_validation->set_rules('oe_stuckOrder', '');
        $this->form_validation->set_rules('np_failure', '');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->model('ns_model');
            $data['tracking_options'] = $this->ns_model->get_ns_tracking();
            $this->load->view('ns/incident_entry_view', $data);
        }
        else
        {            
            $this->load->model('ns_model');
            if($query = $this->ns_model->add_incident())
            {
                redirect('ns/incident_entry');
                //$this->load->view('ns/incident_entry_view', $row);
            }
            else
            {
                $this->load->view('main_menu');            
            }
        }
        
    }

view
Code:
echo form_open('ns/add_incident');

<input type="checkbox" name="oe_related" id="oe_related" value="Y">
<input type="checkbox" name="oe_stuckOrder" id="oe_stuckOrder" value="Y" <?php echo set_checkbox('oe_stuckOrder', 'Y');?> />
<input type="checkbox" name="np_failure" id="np_failure" value="Y" <?php echo set_checkbox('np_failure', 'Y');?> />
</form>
#6

[eluser]techgnome[/eluser]
Technically checkboxes have one of two values... True (non-zero) and False (zero) ... you're specifying the "value" as "Y" ... since it's not 0, that's what's getting passed over. When you uncheck it, it sets the value to 0.. (it doesn't know that you really want "N" ... how could it? there's no way to tell it so)... It wouldn't surprise me to find out that if you uncheck, and then re-check the checkbox, you get either 1 or -1 as the value. That's the way checkboxes work. If you want a different value (like "Y" or "N") stored in your database, then you need to transalate it when reading the form post data.

As a side note, as someone who does do database development during the day, it's generally a bad idea to use string data for this kind of stuff. Use a boolean value in the database... allows you to specify TRUE or FALSE. When you use strings, things get messy and you run the risk of storing an "S" (sometimes) in the field... or "O" (for Often.... And I've seen weirder things). But that's my personal opinion.


-tg
#7

[eluser]CI_Newb[/eluser]
I used to be totally cool with 1 or 0 but the bosses didn't like seeing it so stupid me, rather than just converting it when it was being displayed, had it set to Y/N which at the time of development, I used dreamweaver did by default.

Now that i've learned a lot, I realized how bloated dreamweaver does things so CI is my next step.

Perhaps I should look at other form elements and just get rid of the checkbox.
#8

[eluser]sketchynix[/eluser]
Quote:When you uncheck it, it sets the value to 0.. (it doesn’t know that you really want “N” ... how could it? there’s no way to tell it so)... It wouldn’t surprise me to find out that if you uncheck, and then re-check the checkbox, you get either 1 or -1 as the value.

Not in HTML. The browser will send nothing if the checkbox isn't selected. If it is selected and the value attribute isnt set to anything, the browser will send the variable value as "on".
It is a bit bizarre, but it makes sense if you think of a series of IDs being sent by checkboxes. If one of the IDs is 0 and the user checks the box, the value of the variable would come across as 0, if the browser typically sent a 0 for unchecked boxes, your script would interpret it as not being set. By not sending it at all, you just have to check to see if it exists and thereby have more flexibility in what values you can use in the form.
#9

[eluser]Unknown[/eluser]
Am pleased to see them come to this forum, these messages are useful to me, to thank the landlord. Thanks.
#10

[eluser]techgnome[/eluser]
Now I know why I stayed out of web development professionally... because to me that makes zippo sense. A checkbox is only going to have one of two values... true, false... 1, 0... (putting aside the oddity that is the tri-state checkbox) if it's checked, it's true... value should be 1. If it's unchecked, it's value should be false, or 0. Takes a lot less effort and bandwidth to transmit a 1 or 0 than "some_value_that_may_or_may_not_have_any_meaning_when_I_look_at_it" -- this is where I've seen people get tripped up... they can't separate the data stored from the data displayed and so want their databases and data to look exactly the same as what is seen on the screen. It's just highly inefficient. But eh, what ever, I'm not the one that has to deal with it.

my two centavos.

-tg




Theme © iAndrew 2016 - Forum software by © MyBB