Jump to content

Set database field value if checkbox clicked


Trovars

Recommended Posts

Hi,

I need to make it so the items on my page are only displayed if the value in their database column is set to 1 (and 0 for no display). And I should be able to change those values with a checkbox toggle on my admin panel.

Ideally I wouldn't have to click submit either.

Table:

<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
                      <thead>
                        <tr>
                          <th>ID</th>
                          <th>Item name</th>
                          <th>Item group</th>
                          <th>Actions</th>
                          <th>Display</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        if($result -> num_rows > 0){
                          while($row = $result-> fetch_assoc()){
                            echo "<tr>";
                            echo "<td>" . $row["id"] ."</td>";
                            echo "<td>" . $row["item_name"]. "</td>";
                            echo "<td>" . $row["group_name"] ."</td>";
                            ?>
                            <?php
                            require_once "deleteitem.php";
                            ?>
                            <td>
                              <a href="edititem.php?edit=<?php echo $row['id']; ?>" name="edit"><i class="material-icons">edit</i></a>
                              <a href="?delete=<?php echo $row['id']; ?>" name="delete"><i class="material-icons">delete</i></a>
                            </td>
                            <td>
                            <form action="displayitem.php" method="POST">
                              <div class="togglebutton">
                                <label>
                                  <input type="checkbox" name="display">
                                  <span class="toggle"></span>
                                </label> 
                              </div>
                              </form>
                            </td>
                            <?php
                            echo "</tr>";
                        }
                          }
                        ?>
                      </tbody>
                    </table>

https://i.imgur.com/Qyz6s8v.png

In displayitem.php

<?php
if(isset($_POST['display'])){
    echo '<p>'.$_POST['display'].'</p>';
	// queries etc here
}
?> 

I'm not getting the value here unless I add a submit button.

Guessing jQuery / AJAX is the way to go but can't seem to find any good tutorials on it, are there any other suitable approaches to this?

 

 

 

 

Link to comment
Share on other sites

You can submit the form when the checkbox is clicked

<input type="checkbox" name="display" value="1" onclick="this.form.submit()">   <!-- give it a value -->

The thing to remember about about checkboxes (and radiobuttons) is that only checked values are submitted so your processing has to check if they were posted or not.

Either

$display = isset($_POST['display']) ? $_POST['display'] : 0;

or (v7.0+)

$display = $_POST['display'] ?? 0;

 

Link to comment
Share on other sites

On 8/4/2020 at 10:01 AM, Trovars said:

I need to make it so the items on my page are only displayed if the value in their database column is set to 1

Add this condition into your "select" statement so that it only retrieves the relevant items for displaying.

Don't retrieve everything and then filter it in PHP code.

Regards, 
   Phill  W.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.