I have a modal to edit an inventory item (think office tech like pc, printers, etc.).
- I have an index page listing all the item in a table with separate buttons to view, edit, and delete the row. <- working great!
- On edit I get a modal with textboxes for the fields and can edit and save changes <- working great!
- I've added a toggle switch with text of 'active' and 'inactive' that toggles on click <- working great!
- I now want save the toggle switch 'state' of active/1 or inactive/0 when updating the other fields <- have tried a few options but nothing worked for me
- Problem is I don't have a grasp of the process. The database does have a 'status' field set to bool.
start toggle switch code -
<form method="POST" action="inventory_edit.php?inv_id=<?php echo $row['inv_id'];?>"> <div class="mb-3 row"> <label class ="col-sm-3 col-form-label text-light font-weight-bolder text-right">STATUS</label> <div class="col-sm"> <!-- status toggle switch code - changes color and label text on change --> <label class="switch"> <input type="checkbox" id="togBtn"> <div class="slider round"> <span class="off">ACTIVE</span> <span class="on">INACTIVE</span> </div> </label> </div> </div>
end toggle switch code -
start php update code -
<?php
session_start();
require_once './includes/favicon.php';
include_once('./includes/dbconn.php');
if(isset($_POST['inventory_edit'])){
$database=new Connection();
$db=$database->open();
try{
$inv_id=$_GET['inv_id'];
$inv_item_status=$_POST['inv_item_status'];
$inv_item_make=$_POST['inv_item_make'];
$inv_item_model=$_POST['inv_item_model'];
$inv_item_sn=$_POST['inv_item_sn'];
$sql="UPDATE inventory SET inv_item_status = '$inv_item_status', inv_item_make = '$inv_item_make', inv_item_model = '$inv_item_model', inv_item_sn = '$inv_item_sn' WHERE inv_id = '$inv_id'";
// if-else statement in executing query
$_SESSION['message']=($db->exec($sql))?'Inventory Item Updated Successfully!'
:'ERROR: Inventory Item Not Updated. (PANTHER Error #IE101)';
}
catch(PDOException $e){
$_SESSION['message']=$e->getMessage();
}
// close database connection
$database->close();
}
else{
$_SESSION['message']='All Fields Required!';
}
header('location: inventory_index.php');