Skip to content

Commit 819289c

Browse files
last commit
1 parent 4a3db2c commit 819289c

File tree

5 files changed

+232
-18
lines changed

5 files changed

+232
-18
lines changed

Companies.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@
273273
<thead>
274274
<tr>
275275
<th scope="col" style="width:0%;">#</th>
276-
277276
<th scope="col" style="width:10%;">Name</th>
278277
<th scope="col" style="width:12%;">Image</th>
279278
<th scope="col" style="width:14%;">Phone</th>

CompanyInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@
493493

494494
<div class="col-md-7 col-lg-8 mt-4">
495495
<div class="cardBranch recent-sales overflow-auto">
496-
<div class="card-body" style="font-size: 0.8rem; ">
496+
<div class="card-body" style="font-size: 0.8rem;">
497497
<div class="headerSetting">
498498
<h5 class="card-title">List of employees</h5>
499499
<button id="fixedButtonBranch" type="button" onclick="window.location.href = 'createEmployee.php?id=<?php echo $company_id;?>'" class="btn btn-primary mb-3">Add Employee</button>

createitem.php

Lines changed: 210 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222
$adminEmail = $row2['email'];
2323
}
2424

25-
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
2625

26+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
27+
2728
$item_name = mysqli_real_escape_string($conn, $_POST['item_name']);
2829
$company_FK_item = mysqli_real_escape_string($conn, $_POST['comp_FK_item']);
2930
$box_FK_item = mysqli_real_escape_string($conn, $_POST['box_FK_item']);
3031
$branch_FK_item = mysqli_real_escape_string($conn, $_POST['branch_FK_item']);
3132
$status = mysqli_real_escape_string($conn, $_POST['status']);
3233
$barcode = mysqli_real_escape_string($conn, $_POST['barcode']);
3334

35+
3436
// Check if the box name and barcode already exists in the database
3537
$nameCheck = "SELECT * FROM `item` WHERE `item_name` = '$item_name' AND `barcode`='$barcode'";
3638
$nameCheckResult = $conn->query($nameCheck);
3739

38-
3940
//2 in 1 approach
4041
if ($nameCheckResult->num_rows > 0) {
4142
die("Error: The item name '$item_name' and barcode '$barcode' already exists.");
@@ -45,14 +46,20 @@
4546
VALUES ('$company_FK_item', '$box_FK_item', '$branch_FK_item' ,'$item_name','$status', '$barcode')";
4647

4748
if ($conn->query($sql) === TRUE) {
48-
header("location: showItems.php");
49+
header("location: createitem.php");
50+
51+
$selected_company = isset($_POST['comp_FK_item']) ? $_POST['comp_FK_item'] : ''; // For company selection
52+
4953
} else {
5054
echo "Error: " . $sql . "<br>" . $conn->error;
5155
}
5256

5357
$conn->close();
5458
}
5559

60+
$selected_status = isset($_POST['status']) ? $_POST['status'] : 'default_value';
61+
62+
5663
?>
5764

5865

@@ -419,17 +426,18 @@
419426
<!-- <h5 class="card-title ml-4">Create Company </h5> -->
420427
<div class="card-body">
421428
<br>
429+
422430
<!-- Multi Columns Form -->
423-
<form class="row g-3 needs-validation" action="" method="POST" enctype="multipart/form-data">
431+
<form class="row g-3 needs-validation" action="" method="POST">
424432
<!-- Select Company -->
425433
<div class="col-md-6">
426434
<label for="company">Select Company:</label>
427435
<select id="company" class="form-select" name="comp_FK_item" required>
428436
<option value="">Select a Company</option>
429437
<?php
438+
// Fetch the companies from the database
430439
$result = $conn->query("SELECT comp_id, comp_name FROM compani");
431440
while ($row = $result->fetch_assoc()) {
432-
$selected = ($row['comp_id'] == $selected_company) ? 'selected' : '';
433441
echo "<option value='{$row['comp_id']}' $selected>{$row['comp_name']}</option>";
434442
}
435443
?>
@@ -470,16 +478,16 @@
470478
</div> -->
471479

472480
<div class="col-md-6">
473-
<label for="status" class="form-label">Item Condition</label>
474-
<select class="form-select" id="status" name="status">
475-
<option value="New">New</option>
476-
<option value="Second Hand">Second Hand</option>
477-
<option value="Damaged">Damaged</option>
478-
<option value="Defective">Defective</option>
479-
<option value="Used - Good">Used - Good</option>
480-
<option value="Used - Acceptable">Used - Acceptable</option>
481-
</select>
482-
</div>
481+
<label for="status" class="form-label">Item Condition</label>
482+
<select class="form-select" id="status" name="status">
483+
<option value="New" <?php echo ($selected_status == 'New') ? 'selected' : ''; ?>>New</option>
484+
<option value="Second Hand" <?php echo ($selected_status == 'Second Hand') ? 'selected' : ''; ?>>Second Hand</option>
485+
<option value="Damaged" <?php echo ($selected_status == 'Damaged') ? 'selected' : ''; ?>>Damaged</option>
486+
<option value="Defective" <?php echo ($selected_status == 'Defective') ? 'selected' : ''; ?>>Defective</option>
487+
<option value="Used - Good" <?php echo ($selected_status == 'Used - Good') ? 'selected' : ''; ?>>Used - Good</option>
488+
<option value="Used - Acceptable" <?php echo ($selected_status == 'Used - Acceptable') ? 'selected' : ''; ?>>Used - Acceptable</option>
489+
</select>
490+
</div>
483491
<div class="col-md-6">
484492
<label class="form-label">Barcode</label>
485493
<input type="text" class="form-control" name="barcode" id="item_barcode">
@@ -517,7 +525,7 @@
517525

518526
<script>
519527
$(document).ready(function() {
520-
528+
521529
// When company is changed, fetch the branches
522530
$('#company').change(function() {
523531
var company_id = $(this).val();
@@ -574,8 +582,194 @@
574582
});
575583
});
576584
});
585+
// document.addEventListener('DOMContentLoaded', function () {
586+
// const companySelect = document.getElementById('company');
587+
// const branchSelect = document.getElementById('branch');
588+
589+
// // Retrieve the previously selected company from localStorage
590+
// const selectedCompany = localStorage.getItem('selectedCompany');
591+
// if (selectedCompany) {
592+
// companySelect.value = selectedCompany;
593+
// }
594+
595+
// // Store the selected company in localStorage on change
596+
// companySelect.addEventListener('change', function () {
597+
// localStorage.setItem('selectedCompany', this.value);
598+
599+
// // Retrieve the previously selected branch from localStorage
600+
// const selectedBranch = localStorage.getItem('selectedBranch');
601+
// if (selectedBranch) {
602+
// branchSelect.value = selectedBranch;
603+
// }
604+
605+
// // Store the selected branch in localStorage on change
606+
// branchSelect.addEventListener('change', function () {
607+
// localStorage.setItem('selectedBranch', this.value);
608+
// });
609+
// });
610+
// });
611+
612+
613+
// // Restore selected company and branch on page load
614+
// window.onload = function() {
615+
// const selectedCompany = localStorage.getItem('selectedCompany');
616+
// const selectedBranch = localStorage.getItem('selectedBranch');
617+
618+
// if (selectedCompany) {
619+
// document.getElementById('company').value = selectedCompany;
620+
// loadBranches(selectedCompany); // Load branches based on the company
621+
// }
622+
623+
// if (selectedBranch) {
624+
// document.getElementById('branch').value = selectedBranch;
625+
// loadBoxes(selectedBranch); // Load boxes based on the branch
626+
// }
627+
// };
628+
629+
// // Save selections to localStorage
630+
// document.getElementById('company').addEventListener('change', function() {
631+
// const company = this.value;
632+
// localStorage.setItem('selectedCompany', company);
633+
// loadBranches(company);
634+
// });
635+
636+
// document.getElementById('branch').addEventListener('change', function() {
637+
// const branch = this.value;
638+
// localStorage.setItem('selectedBranch', branch);
639+
// loadBoxes(branch);
640+
// });
641+
642+
// // Function to load branches based on the selected company
643+
// function loadBranches(companyId) {
644+
// if (companyId) {
645+
// const xhr = new XMLHttpRequest();
646+
// xhr.open('GET', 'getBranches.php?companyId=' + companyId, true);
647+
// xhr.onload = function() {
648+
// if (xhr.status === 200) {
649+
// document.getElementById('branch').innerHTML = xhr.responseText;
650+
// }
651+
// };
652+
// xhr.send();
653+
// }
654+
// }
655+
656+
// // Function to load boxes based on the selected branch
657+
// function loadBoxes(branchId) {
658+
// if (branchId) {
659+
// const xhr = new XMLHttpRequest();
660+
// xhr.open('GET', 'getBoxes.php?branchId=' + branchId, true);
661+
// xhr.onload = function() {
662+
// if (xhr.status === 200) {
663+
// document.getElementById('box').innerHTML = xhr.responseText;
664+
// }
665+
// };
666+
// xhr.send();
667+
// }
668+
// }
577669
</script>
578670

671+
<script>
672+
673+
document.addEventListener('DOMContentLoaded', function () {
674+
const companySelect = document.getElementById('company');
675+
const branchSelect = document.getElementById('branch');
676+
const boxSelect=document.getElementById('box');
677+
// Retrieve the previously selected company from localStorage
678+
const selectedCompany = localStorage.getItem('selectedCompany');
679+
if (selectedCompany) {
680+
companySelect.value = selectedCompany;
681+
loadBranches(selectedCompany); // Load branches based on the selected company
682+
}
683+
684+
// Store the selected company in localStorage on change
685+
companySelect.addEventListener('change', function () {
686+
localStorage.setItem('selectedCompany', this.value);
687+
loadBranches(this.value); // Load branches based on the new selection
688+
});
689+
690+
// Retrieve the previously selected branch from localStorage
691+
const selectedBranch = localStorage.getItem('selectedBranch');
692+
if (selectedBranch) {
693+
branchSelect.value = selectedBranch;
694+
}
695+
696+
// Store the selected branch in localStorage on change
697+
branchSelect.addEventListener('change', function () {
698+
localStorage.setItem('selectedBranch', this.value);
699+
});
700+
701+
// Retrieve the previously selected branch from localStorage
702+
const selectedBox = localStorage.getItem('selectedBox');
703+
if (selectedBox) {
704+
boxSelect.value = selectedBox;
705+
}
706+
707+
// Store the selected branch in localStorage on change
708+
boxSelect.addEventListener('change', function () {
709+
localStorage.setItem('selectedBox', this.value);
710+
});
711+
712+
// Function to load branches via AJAX
713+
function loadBranches(company_id) {
714+
$.ajax({
715+
url: 'get_branches.php',
716+
type: 'POST',
717+
data: {
718+
company_id: company_id
719+
},
720+
success: function (response) {
721+
try {
722+
var branches = JSON.parse(response);
723+
// Clear existing branches
724+
branchSelect.innerHTML = '<option value="">Select a Branch</option>';
725+
// Add the new options from the response
726+
branches.forEach(function (branch) {
727+
branchSelect.innerHTML += '<option value="' + branch.branch_id + '">' + branch.branch_name + '</option>';
728+
});
729+
730+
// If a branch was previously selected, set it again
731+
const selectedBranch = localStorage.getItem('selectedBranch');
732+
if (selectedBranch) {
733+
branchSelect.value = selectedBranch;
734+
}
735+
} catch (e) {
736+
console.error("Invalid JSON response", response);
737+
}
738+
}
739+
});
740+
}
741+
742+
// Function to load boxes via AJAX
743+
function loadBoxes(company_id) {
744+
$.ajax({
745+
url: 'get_boxes.php',
746+
type: 'POST',
747+
data: {
748+
company_id: company_id
749+
},
750+
success: function (response) {
751+
try {
752+
var box = JSON.parse(response);
753+
// Clear existing boxs
754+
boxSelect.innerHTML = '<option value="">Select a box</option>';
755+
// Add the new options from the response
756+
br.forEach(function (branch) {
757+
branchSelect.innerHTML += '<option value="' + branch.branch_id + '">' + branch.branch_name + '</option>';
758+
});
759+
760+
// If a branch was previously selected, set it again
761+
const selectedBranch = localStorage.getItem('selectedBranch');
762+
if (selectedBranch) {
763+
branchSelect.value = selectedBranch;
764+
}
765+
} catch (e) {
766+
console.error("Invalid JSON response", response);
767+
}
768+
}
769+
});
770+
}
771+
});
772+
</script>
579773
<script>
580774
const dataTable = new simpleDatatables.DataTable("#myTable2", {
581775
searchable: false,

default value/new.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
// Simulate the previously selected value (from database or after form submission)
3+
$previous_value = isset($_POST['my_select']) ? $_POST['my_select'] : 'default_value';
4+
$previous_value2 = isset($_POST['my_select2']) ? $_POST['my_select2'] : 'default_value';
5+
?>
6+
7+
<form action="" method="POST">
8+
<label for="my_select">Choose an 1 option:</label>
9+
<select name="my_select" id="my_select">
10+
<option value="option1" <?= $previous_value == 'option1' ? 'selected' : '' ?>>Option 1</option>
11+
<option value="option2" <?= $previous_value == 'option2' ? 'selected' : '' ?>>Option 2</option>
12+
<option value="option3" <?= $previous_value == 'option3' ? 'selected' : '' ?>>Option 3</option>
13+
</select>
14+
<label for="my_select">Choose an 2 option:</label>
15+
<select name="my_select2" id="my_select2">
16+
<option value="option11" <?= $previous_value2 == 'option11' ? 'selected' : '' ?>>Option 11</option>
17+
<option value="option22" <?= $previous_value2== 'option22' ? 'selected' : '' ?>>Option 21</option>
18+
<option value="option33" <?= $previous_value2 == 'option33' ? 'selected' : '' ?>>Option 31</option>
19+
</select>
20+
<input type="submit" value="Submit">
21+
</form>

image/guard_rice.jpg

3.4 KB
Loading

0 commit comments

Comments
 (0)