Aram Mkrtchyan
Aram Mkrtchyan

Reputation: 2700

Php update checkbox states in db

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 :)

Upvotes: 1

Views: 5938

Answers (4)

avisheks
avisheks

Reputation: 1180

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
}

Upvotes: 0

Pratik Joshi
Pratik Joshi

Reputation: 11693

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.

Upvotes: 0

spandanaz
spandanaz

Reputation: 21

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.

Upvotes: 2

Mayur Relekar
Mayur Relekar

Reputation: 67

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

and work your way from there

Upvotes: 0

Related Questions