Kris
Kris

Reputation: 109

Checkbox won't update from HTML form

Here my edit script on HTML form (my-edit-file):

<input type="hidden" name=<?php echo "toolbox" ?> <?php if($value['toolbox'] == "0") 
echo "unchecked='unchecked'"; ?> value="1" style="width: 40px;"/>

<input type="checkbox" name=<?php echo "toolbox" ?> <?php if($value['toolbox'] == "1") 
echo "checked='checked'"; ?> value="<?=$value['toolbox']?>" style="width: 40px;" />

My model update script always captured the second value "the unhidden" checkbox form. When the first data was "0" then I want to update by clicked the checkbox it should be updated to "1" but my models only captured the second checkbox form.

MyController:

public function update_data()
  {

    $role = $this->session->userdata('role_id');

    $id           = $this->input->post('id');
    $toolbox      = $this->input->post('toolbox');

   $query = $this->m_urfave->update_data($id, $toolbox);

    if ($query > 0) {
        }
    $this->session->set_flashdata('Msg', '<b><h3><font color="blue">Data updated</font)</h3></b>');
    redirect('urfave');
}

MyModels:

function update_data($id, $toolbox) 
{
  $query=$this->db->query("update favorite_tbl SET toolbox='$toolbox' where id='$id'");

 }

Upvotes: 0

Views: 213

Answers (1)

BEingprabhU
BEingprabhU

Reputation: 1686

Firstly you have to modify your code as below

<form action="url-to-update-method" method="post">
   //this hidden field is to send 0 if the checkbox is unchecked as 
   //php post variable will not have checkbox value
    <input type="hidden" name="toolbox" value="0"/>

   //This checkbox is the one which is visible on the HTML page to check
  <input type="checkbox" name="toolbox" value="1" 
  <?php if($value['toolbox'] == "1"){ echo "checked='checked'"; } ?> />
</form>

In your code, the hidden input field has a "checked" attribute. There is no such attribute. Please correct it.

It may help you.

Upvotes: 1

Related Questions