0

please help me i would like to delete some check box if checkbox unchecked.

My checkbox is automatic show from database my sql. and i would like to delete some checkbox when checkbox in not checked. PLease Help

its my controller

public function edit_overview($id_product)
{
    if ($this->input->post('submit')) {
        foreach ($id_overview = $this->input->post('id_overview') as $rm) {
            $check_idoverview = $this->Biostar->check_idoverview($id_product, $rm);
            if ($check_idoverview==unchecked){
                $data['file'] = $check_idoverview;
                $this->Biostar->delete_overview($check_idoverview,$id_product);
            }else{

            if ($check_idoverview > 0) {

            } else {
                $datafield = array(
                    'id_product' => $id_product,
                    'id_overview' => $rm
                );
                $this->Biostar->saveoverviewproduct($datafield);
                $message_success = "Data Has Been Update";
            }
            }
        }
    }
    $data['message_success'] = @$message_success;
    $field = $this->Biostar->get_overview($id_product);
    $fieldid_product = $this->Biostar->get_id_product($id_product);
    $data['field'] = $field;
    $data['id_product'] = $fieldid_product;
    $data['content'] = "biostar/edit_overview_product";
    $this->load->view('dashboard/index', $data);
}

My Model

function delete_overview($check_overview,$id_product)
{
    $sql = "delete from overview_briostar where id_overview='$check_overview' AND id_product='$id_product'";
    return $sql;
}

My View

<form method="post" action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $id_product->id_product; ?>">
            <div class="box-body">
                <?php foreach ($speed as $row){ ?>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="id_overview[]" onClick="EnableSubmit3(this)" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?>
                        </label>
                    </div>
                <?php } ?>
                <!-- /input-group -->
            </div>
            <div class="box-footer">
                <input value="Submit" type="submit" id="submit3" name="submit" class="btn btn-primary">
            </div>
                </form>
3
  • what do you mean by delete unchecked checkbox.please explain clearly to get any help here Commented Sep 17, 2016 at 9:05
  • emmm. if the checkbox in unchecked i would like to delete it Commented Sep 17, 2016 at 9:08
  • You delete that record which is unchecked ? You can try deleting those ids that are not posted from the all records Commented Sep 17, 2016 at 9:45

2 Answers 2

1

Unchecked checkbox value will not get posted, so you have to use jquery and ajax

Your checkbox

<input class="id_overview" type="checkbox" name="id_overview[]" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?>

your jquery

<script type="text/javascript">
    $(document).ready(function() {
        $("form#frm").submit(function(e) { // give a id to your form
            e.preventDefault();
            var ids = new Array();
            $('.id_overview').each(function() {
                if ($(this).is(':checked')) {
                } else {
                    ids.push($(this).val());
                }
            });
            $.ajax({
                // send ids through your ajax code
            });
        });
    });
</script>

create a new function and call it by ajax to delete the ids.

Sign up to request clarification or add additional context in comments.

2 Comments

i dont understand ajax script :(
Using the jquery, we are checking the checkbox is checked or not, if uncheck then push to an array. This ids will be pass to an new function of controller using ajax.
0

You can try like this

<form method="post" action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $id_product->id_product; ?>">
        <div class="box-body">
            <?php foreach ($speed as $row){ ?>
                <div class="checkbox">
                    <label>
                        <input type="hidden" name="all_id[]" value="<?php echo $row['id_overview']; ?>" />
                        <input type="checkbox" name="id_overview<?php echo $row['id_overview']; ?>" onClick="EnableSubmit3(this)" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?>
                    </label>
                </div>
            <?php } ?>
            <!-- /input-group -->
        </div>
        <div class="box-footer">
            <input value="Submit" type="submit" id="submit3" name="submit" class="btn btn-primary">
        </div>
            </form>

and in you controller

if ($this->input->post('submit')) {
foreach($all_id as $row_id)
{
     if($this->input->post('id_overview'.$row_id))
     {
           //do action where id is present
     }
     else
     {
            //do action if unchecked   by getting id $row_id
     }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.