Trovars Posted August 4, 2020 Share Posted August 4, 2020 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 4, 2020 Share Posted August 4, 2020 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; Quote Link to comment Share on other sites More sharing options...
Phi11W Posted August 6, 2020 Share Posted August 6, 2020 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.