|
22 | 22 | $adminEmail = $row2['email'];
|
23 | 23 | }
|
24 | 24 |
|
25 |
| -if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
26 | 25 |
|
| 26 | +if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
| 27 | + |
27 | 28 | $item_name = mysqli_real_escape_string($conn, $_POST['item_name']);
|
28 | 29 | $company_FK_item = mysqli_real_escape_string($conn, $_POST['comp_FK_item']);
|
29 | 30 | $box_FK_item = mysqli_real_escape_string($conn, $_POST['box_FK_item']);
|
30 | 31 | $branch_FK_item = mysqli_real_escape_string($conn, $_POST['branch_FK_item']);
|
31 | 32 | $status = mysqli_real_escape_string($conn, $_POST['status']);
|
32 | 33 | $barcode = mysqli_real_escape_string($conn, $_POST['barcode']);
|
33 | 34 |
|
| 35 | + |
34 | 36 | // Check if the box name and barcode already exists in the database
|
35 | 37 | $nameCheck = "SELECT * FROM `item` WHERE `item_name` = '$item_name' AND `barcode`='$barcode'";
|
36 | 38 | $nameCheckResult = $conn->query($nameCheck);
|
37 | 39 |
|
38 |
| - |
39 | 40 | //2 in 1 approach
|
40 | 41 | if ($nameCheckResult->num_rows > 0) {
|
41 | 42 | die("Error: The item name '$item_name' and barcode '$barcode' already exists.");
|
|
45 | 46 | VALUES ('$company_FK_item', '$box_FK_item', '$branch_FK_item' ,'$item_name','$status', '$barcode')";
|
46 | 47 |
|
47 | 48 | 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 | + |
49 | 53 | } else {
|
50 | 54 | echo "Error: " . $sql . "<br>" . $conn->error;
|
51 | 55 | }
|
52 | 56 |
|
53 | 57 | $conn->close();
|
54 | 58 | }
|
55 | 59 |
|
| 60 | +$selected_status = isset($_POST['status']) ? $_POST['status'] : 'default_value'; |
| 61 | + |
| 62 | + |
56 | 63 | ?>
|
57 | 64 |
|
58 | 65 |
|
|
419 | 426 | <!-- <h5 class="card-title ml-4">Create Company </h5> -->
|
420 | 427 | <div class="card-body">
|
421 | 428 | <br>
|
| 429 | + |
422 | 430 | <!-- 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"> |
424 | 432 | <!-- Select Company -->
|
425 | 433 | <div class="col-md-6">
|
426 | 434 | <label for="company">Select Company:</label>
|
427 | 435 | <select id="company" class="form-select" name="comp_FK_item" required>
|
428 | 436 | <option value="">Select a Company</option>
|
429 | 437 | <?php
|
| 438 | + // Fetch the companies from the database |
430 | 439 | $result = $conn->query("SELECT comp_id, comp_name FROM compani");
|
431 | 440 | while ($row = $result->fetch_assoc()) {
|
432 |
| - $selected = ($row['comp_id'] == $selected_company) ? 'selected' : ''; |
433 | 441 | echo "<option value='{$row['comp_id']}' $selected>{$row['comp_name']}</option>";
|
434 | 442 | }
|
435 | 443 | ?>
|
|
470 | 478 | </div> -->
|
471 | 479 |
|
472 | 480 | <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> |
483 | 491 | <div class="col-md-6">
|
484 | 492 | <label class="form-label">Barcode</label>
|
485 | 493 | <input type="text" class="form-control" name="barcode" id="item_barcode">
|
|
517 | 525 |
|
518 | 526 | <script>
|
519 | 527 | $(document).ready(function() {
|
520 |
| - |
| 528 | + |
521 | 529 | // When company is changed, fetch the branches
|
522 | 530 | $('#company').change(function() {
|
523 | 531 | var company_id = $(this).val();
|
|
574 | 582 | });
|
575 | 583 | });
|
576 | 584 | });
|
| 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 | + // } |
577 | 669 | </script>
|
578 | 670 |
|
| 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> |
579 | 773 | <script>
|
580 | 774 | const dataTable = new simpleDatatables.DataTable("#myTable2", {
|
581 | 775 | searchable: false,
|
|
0 commit comments