1

I am new in php, i have a question. How can I update checkbox states in db? checkboxes have same name. when click on update button it will update all row checkboxes stat in db. and checkboxes in db get true or false

html part

<div class='blocks-output'>
    <form action='/admin/blocks' method='post'>
        <input type='hidden' name='updated_block_index' value=''/>
        <input type='hidden' name='updated_block_name' value=''/>
        <table class='table table-hover table-striped table-bordered'>
            <thead>
                <tr class='text-uppercase'>
                    <td>#</td>
                    <td>selected</td>
                    <td>block index</td>
                    <td>block name</td>
                    <td>edit</td>
                    <td>delete</td>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($blocksInfo as $row) { ?>
                    <tr>
                        <td>
                            <?php echo $row['id']; ?>
                        </td>
                        <td>
                            <input type="checkbox" name='id' value='<?php echo $row['id']; ?>' <?php if($row['selected'] == 1) echo 'checked'; ?> />
                        </td>
                        <td>
                            <p><?php echo $row['block_index']; ?></p>

                            <div class='form-group hidea'>
                                <input type='number' class='form-control' name='block_index' placeholder='Block index' value='<?php echo $row['block_index']; ?>'>
                            </div>
                        </td>
                        <td>
                            <p><?php echo $row['block_name']; ?></p>

                            <div class='form-group hidea'>
                                <input type='text' class='form-control' name='block_name' placeholder='Block name' value='<?php echo $row['block_name']; ?>'>
                            </div>
                        </td>
                        <td>
                            <a href='' class='edit-row btn btn-primary' role='button'>edit</a>
                            <button class='update-row btn btn-primary' name='update_row' value='<?php echo $row['id']; ?>'>update</button>
                        </td>
                        <td>
                            <button type='submit' class='btn btn-primary' name='delete_row' value='<?php echo $row['id']; ?>'>delete</button>
                        </td>
                    </tr>
                <?php } ?>

                <tr>
                    <td>
                        <input type='submit' class='btn btn-primary' name='clear_table' value='delete all'>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td ></td>
                    <td>
                        <input type='submit' class='btn btn-primary' name='update_table' value='update'>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</div>

enter image description here

thank you :)

5
  • you get array , whats the problem ? Commented Dec 26, 2014 at 8:23
  • i just don't know how to do that Commented Dec 26, 2014 at 8:23
  • change the checkbox name='id' to name='id[]' and get the checkbox in array value.. Commented Dec 26, 2014 at 8:26
  • use name='id[]' insted of name='id' for your checkboxes,then you will get an array of selected values on submit.Update the table using that Commented Dec 26, 2014 at 8:27
  • what is exact problem you are facing? Commented Dec 26, 2014 at 8:42

4 Answers 4

2

Replace 'id' for the name to below.

 <input type="checkbox" name='<?php echo $row['id']; ?>' value='<?php echo $row['id']; ?>' <?php if($row['selected'] == 1) echo 'checked'; ?> />

When you declared Name='id' all the elements have been picking on the same name, after the above declaration, each checkbox will have a unique name equal to the ID.

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

1 Comment

good answer.....everyone is saying that keep id array i don't know why they are saying even they have unique id value...anyways nice answer
0

Nothing here that a few simple google searches won't answer. You could start with this:

and work your way from there

1 Comment

I am searching but not found anything that i want
0

Use array like: id[] and each checkbox should be given value like their ID in table.

So it will be in short <input type="checkbox" name='id[]' value='<?php echo $row['id']; ?>' <?php if($row['selected'] == 1) echo 'checked'; ?> />

So after post You will get only checkboxes which were checked , so accordingly save data in DB.

Comments

0

That's simple something like this: form.php

<html>
<body>
<form action="action.php" method="POST">
<label class="checkbox inline">
<input type="checkbox" name="frm_update" id="frm_update" value="<?=$id>"> check this box
</label>
</form>
</body>
</html>

action.php

<?php
if(isset($_POST['frm_update'])){
  //do what you wanted to do. Save it to database etc
}

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.