Skip to content

Commit 66c5472

Browse files
fix: "drop down not working"
1 parent 79cc66a commit 66c5472

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

createitem.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// session_start(); // Start the session
44
session_start();
55

6-
76
// Check if the user is logged in
87
if (!isset($_SESSION['email'])) {
98
// If not logged in, redirect to login page
@@ -24,6 +23,7 @@
2423
}
2524

2625
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
26+
2727
$item_name = mysqli_real_escape_string($conn, $_POST['item_name']);
2828
$company_FK_item = mysqli_real_escape_string($conn, $_POST['comp_FK_item']);
2929
$box_FK_item = mysqli_real_escape_string($conn, $_POST['box_FK_item']);
@@ -35,6 +35,8 @@
3535
$nameCheck = "SELECT * FROM `item` WHERE `item_name` = '$item_name' AND `barcode`='$barcode'";
3636
$nameCheckResult = $conn->query($nameCheck);
3737

38+
39+
//2 in 1 approach
3840
if ($nameCheckResult->num_rows > 0) {
3941
die("Error: The item name '$item_name' and barcode '$barcode' already exists.");
4042
}
@@ -48,6 +50,27 @@
4850
echo "Error: " . $sql . "<br>" . $conn->error;
4951
}
5052

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+
5174
$conn->close();
5275
}
5376

@@ -424,34 +447,36 @@
424447
<input type="checkbox" id="cancelAutoSelection" name="cancelAutoSelection">
425448
<label for="cancelAutoSelection">Cancel Auto-Selection</label>
426449

450+
<!-- Select Company -->
427451
<div class="col-md-6">
428452
<label for="company">Select Company:</label>
429453
<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>
432455
<?php
433-
434-
//fetch companies
435456
$result = $conn->query("SELECT comp_id, comp_name FROM compani");
436457
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>";
438460
}
439461
?>
440-
441462
</select>
442463
</div>
443464

465+
<!-- Select Branch -->
444466
<div class="col-md-6">
445467
<label for="branch">Select a Branch:</label>
446468
<select id="branch" class="form-select" name="branch_FK_item" required>
447469
<option value="">Select a Branch</option>
470+
<!-- The options will be populated via AJAX based on the selected company -->
448471
</select>
449472
</div>
450473

474+
<!-- Select Box -->
451475
<div class="col-md-6">
452476
<label for="box">Select Box:</label>
453477
<select id="box" class="form-select" name="box_FK_item" required>
454478
<option value="">Select a Box</option>
479+
<!-- The options will be populated via AJAX based on the selected branch -->
455480
</select>
456481
</div>
457482

0 commit comments

Comments
 (0)