Welcome Guest, Not a member yet? Register   Sign In
storing radio button???
#1

[eluser]Tom Taila[/eluser]
Hey guys, Im having a lil trouble storing part of a form which is a radio button, everything else is storing into the database just fine, but this isnt for some reason, below is the html and codeigniter code for the button/storage funtion:

Code:
<span id="radio">Bottle size: <br/></span>
                            <span id="radio2">1 bottle / $49.95: </span>
                            &lt;input class="validate[required] radio" type="radio" name="data[User][preferedColor]"  id="radio2"  value="1" name="tick1"/&gt;
                            <br/>
                            <span id="radio3">3 bottle / $99.90: </span>
                            &lt;input class="validate[required] radio" type="radio" name="data[User][preferedColor]"  id="radio2"  value="3" name="tick2"/&gt;

and heres the ci part:

Code:
function save_info()
    {
        $customer_info = array(
            'full_name' => $this->input->post('full_name'),
            'address' => $this->input->post('address'),
            'title' => $this->input->post('title'),
            'city' => $this->input->post('city'),
            'zip_code' => $this->input->post('zip_code'),
            'quantity' => $this->input->post('tick1'),
            'quantity' => $this->input->post('tick2')
        );
        
        $data = $this->db->insert('customer_info', $customer_info);
    }




the database is set up saying that the field in the table it will store in is called 'quantity' and its type is tiny int,

any help would much appreciated, thanks so much! Smile
#2

[eluser]smilie[/eluser]
Well, first of all, you have 2 x 'name' in your buttons, which I pressume will break something :-)

Secondly (more general HTML) is that radio buttons (just as well as check boxes) have 2 states:
TRUE (checked)
FALSE (not checked)

So, what you could do is:

Code:
function save_info()
    {
        $customer_info = array(
            'full_name' => $this->input->post('full_name'),
            'address' => $this->input->post('address'),
            'title' => $this->input->post('title'),
            'city' => $this->input->post('city'),
            'zip_code' => $this->input->post('zip_code'),
            'quantity' => (($this->input->post('tick1')) ? '1' : '0');,
            'quantity' => (($this->input->post('tick2')) ? '1' : '0');
        );
        
        $data = $this->db->insert('customer_info', $customer_info);

This will place 1 or 0 in your database for the radio buttons. Ofcourse, you may set anything else instead of 1 and 0 (yes / no; true / false; kill / do not kill etc...)

By the way, I find it also strange that you have 2 x quantity in your database :-)

Regards,
Smilie
#3

[eluser]CroNiX[/eluser]
To build on what smiley said... Certain HTML controls, such as checkboxes and radio buttons, ONLY send their data if they are checked/ticked.

If you examine your post data, you will notice that an unchecked checkbox will NOT be submitted with the form data. It just doesn't even get transmitted. This is normal HTML and according to specs. In THOSE cases, you need to test to see if the checkbox WAS NOT submitted, and if it wasn't, manually assign it a value of 0.
#4

[eluser]Tom Taila[/eluser]
sorry this is way overdue, but thank you both so much, very rude of me for not replying earlier but better late then never, thanks again




Theme © iAndrew 2016 - Forum software by © MyBB