|
3 | 3 | // session_start(); // Start the session
|
4 | 4 | session_start();
|
5 | 5 |
|
6 |
| - |
7 | 6 | // Check if the user is logged in
|
8 | 7 | if (!isset($_SESSION['email'])) {
|
9 | 8 | // If not logged in, redirect to login page
|
|
24 | 23 | }
|
25 | 24 |
|
26 | 25 | if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
| 26 | + |
27 | 27 | $item_name = mysqli_real_escape_string($conn, $_POST['item_name']);
|
28 | 28 | $company_FK_item = mysqli_real_escape_string($conn, $_POST['comp_FK_item']);
|
29 | 29 | $box_FK_item = mysqli_real_escape_string($conn, $_POST['box_FK_item']);
|
|
35 | 35 | $nameCheck = "SELECT * FROM `item` WHERE `item_name` = '$item_name' AND `barcode`='$barcode'";
|
36 | 36 | $nameCheckResult = $conn->query($nameCheck);
|
37 | 37 |
|
| 38 | + |
| 39 | + //2 in 1 approach |
38 | 40 | if ($nameCheckResult->num_rows > 0) {
|
39 | 41 | die("Error: The item name '$item_name' and barcode '$barcode' already exists.");
|
40 | 42 | }
|
|
48 | 50 | echo "Error: " . $sql . "<br>" . $conn->error;
|
49 | 51 | }
|
50 | 52 |
|
| 53 | + |
| 54 | + //for auto selection of companies, branch, box dropdown |
| 55 | + |
| 56 | + |
| 57 | + if (isset($_POST['cancelAutoSelection'])) { |
| 58 | + // Unset session data to cancel auto-selection |
| 59 | + unset($_SESSION['last_company'], $_SESSION['last_branch'], $_SESSION['last_box']); |
| 60 | + } else { |
| 61 | + // Store the selected values in session variables |
| 62 | + $_SESSION['last_company'] = $_POST['comp_FK_item']; |
| 63 | + $_SESSION['last_branch'] = $_POST['branch_FK_item']; |
| 64 | + $_SESSION['last_box'] = $_POST['box_FK_item']; |
| 65 | + } |
| 66 | + |
| 67 | + // Set default values for the company, branch, and box dropdowns |
| 68 | + $selected_company = isset($_SESSION['last_company']) ? $_SESSION['last_company'] : ''; |
| 69 | + $selected_branch = isset($_SESSION['last_branch']) ? $_SESSION['last_branch'] : ''; |
| 70 | + $selected_box = isset($_SESSION['last_box']) ? $_SESSION['last_box'] : ''; |
| 71 | + |
| 72 | + |
| 73 | + |
51 | 74 | $conn->close();
|
52 | 75 | }
|
53 | 76 |
|
|
424 | 447 | <input type="checkbox" id="cancelAutoSelection" name="cancelAutoSelection">
|
425 | 448 | <label for="cancelAutoSelection">Cancel Auto-Selection</label>
|
426 | 449 |
|
| 450 | + <!-- Select Company --> |
427 | 451 | <div class="col-md-6">
|
428 | 452 | <label for="company">Select Company:</label>
|
429 | 453 | <select id="company" class="form-select" name="comp_FK_item" required>
|
430 |
| - <option value=""> Select a Company </option> |
431 |
| - |
| 454 | + <option value="">Select a Company</option> |
432 | 455 | <?php
|
433 |
| - |
434 |
| - //fetch companies |
435 | 456 | $result = $conn->query("SELECT comp_id, comp_name FROM compani");
|
436 | 457 | while ($row = $result->fetch_assoc()) {
|
437 |
| - echo "<option value='{$row['comp_id']}'>{$row['comp_name']}</option>"; |
| 458 | + $selected = ($row['comp_id'] == $selected_company) ? 'selected' : ''; |
| 459 | + echo "<option value='{$row['comp_id']}' $selected>{$row['comp_name']}</option>"; |
438 | 460 | }
|
439 | 461 | ?>
|
440 |
| - |
441 | 462 | </select>
|
442 | 463 | </div>
|
443 | 464 |
|
| 465 | + <!-- Select Branch --> |
444 | 466 | <div class="col-md-6">
|
445 | 467 | <label for="branch">Select a Branch:</label>
|
446 | 468 | <select id="branch" class="form-select" name="branch_FK_item" required>
|
447 | 469 | <option value="">Select a Branch</option>
|
| 470 | + <!-- The options will be populated via AJAX based on the selected company --> |
448 | 471 | </select>
|
449 | 472 | </div>
|
450 | 473 |
|
| 474 | + <!-- Select Box --> |
451 | 475 | <div class="col-md-6">
|
452 | 476 | <label for="box">Select Box:</label>
|
453 | 477 | <select id="box" class="form-select" name="box_FK_item" required>
|
454 | 478 | <option value="">Select a Box</option>
|
| 479 | + <!-- The options will be populated via AJAX based on the selected branch --> |
455 | 480 | </select>
|
456 | 481 | </div>
|
457 | 482 |
|
|
0 commit comments