Mam formularz jak poniżej, który wyświetla mi pozycje dokumentu WZ pobranego z bazy MSSQL.
Wyświetlane są pozycje jedna za drugą, tzn. Najpierw pierwsza, potem druga itd...
Jest prosta logika, pusty nie uzupełniony formularz - nie ma żadnego podświetlenia, gdy uzupełnię wartość w polu CHECK i jest zgodna z polem ILOŚĆ - całość zmienia się na zielono i przechodzić ma do następnej pozycji. Gdy wartości się różnią to formularz zmienia się na czerwono - ma nie pozwalać na zapis danych do bazy póki ilość nie będzie zgodna.
Uzupełniam pole CHECK i daję zapisz, powinno się zapisywać do bazy MySQL za każdym razem i to się dzieje, ale...
gdy przechodzę na druga pozycję to podświetla na czerwono jakby sprawdzał pole ILOŚĆ z poprzednim wpisem do pola CHECK.
a jeżeli pole ILOŚĆ jest takie same jak pole CHECK w poprzednim wpisie to blokuje mi możliwość wpisania. Trochę pokręcone tłumaczenie ale może z kodu da się zrozumieć.
Proszę o pomoc jak to ogarnąć żeby po przejściu do następnej pozycji nie brał pod uwagę ilości z poprzedniego wpisu. Aha, jeszcze jedno, wyświetla w kółko ten same pozycje w kółko - nie chce zakończyć na ostatniej pozycji

Poniżej mój kod i, oraz skrypty:

  1. <?php
  2.  
  3. if (!isset($_SESSION['user_name'])) {
  4. header("Location: index.php");
  5. exit();
  6. }
  7.  
  8. ini_set('display_errors', 1);
  9. ini_set('display_startup_errors', 1);
  10.  
  11. require 'db-connect.php';
  12. require 'mysql-connect.php';
  13.  
  14. $documentNumber = $_GET['doc'] ?? '';
  15.  
  16. if (!isset($_SESSION['last_document']) || $_SESSION['last_document'] !== $documentNumber) {
  17. $_SESSION['current_position'] = 0;
  18. $_SESSION['last_document'] = $documentNumber;
  19. }
  20.  
  21. $operationDate = '';
  22. if (!isset($_SESSION['current_position'])) {
  23. $_SESSION['current_position'] = 0;
  24. }
  25. $currentPosition = $_SESSION['current_position'];
  26.  
  27. $sql = "SELECT * FROM DB_NAME WHERE WZ_NR = ?";
  28. $params = array($documentNumber);
  29. $stmt = sqlsrv_query($conn, $sql, $params);
  30.  
  31. if ($stmt === false) {
  32. die(print_r(sqlsrv_errors(), true));
  33. }
  34.  
  35. $positions = [];
  36. while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
  37. $positions[] = $row;
  38. }
  39. $totalPositions = count($positions);
  40.  
  41. if ($totalPositions == 0) {
  42. echo "<p>Nie znalezono pozycji dla dokumentu.</p>";
  43. exit();
  44. }
  45. Set current position
  46. $currentRow = $positions[$currentPosition];
  47. $positionNumber = $currentRow['lp'];
  48.  
  49. if (isset($_POST['save'])) {
  50. $checkValue = floatval($_POST['check'][$currentPosition] ?? 0);
  51. $quantity = floatval($_POST['quantity'][$currentPosition] ?? 0);
  52. $productShortcut = $_POST['productShortcut'][$currentPosition] ?? '';
  53. $description = $_POST['description'][$currentPosition] ?? '';
  54. $userName = $_SESSION['user_name'];
  55.  
  56. $sqlInsert = "INSERT INTO wz_records (document_number, operation_date, product_shortcut, description, quantity, check_value, save_date, position_number, user_name)
  57. VALUES (?, ?, ?, ?, ?, ?, NOW(), ?, ?)
  58. ON DUPLICATE KEY UPDATE check_value = VALUES(check_value), save_date = NOW()";
  59.  
  60. $stmtInsert = $mysqli->prepare($sqlInsert);
  61. if ($stmtInsert) {
  62. $stmtInsert->bind_param("ssssddss", $documentNumber, $operationDate, $productShortcut, $description, $quantity, $checkValue, $positionNumber, $userName);
  63. $stmtInsert->execute();
  64. $stmtInsert->close();
  65. }
  66.  
  67. $_SESSION['current_position']++;
  68. if ($_SESSION['current_position'] >= $totalPositions) {
  69. echo "<script>alert('To jest ostatnia pozycja.');</script>";
  70. $_SESSION['current_position'] = 0;
  71. }
  72.  
  73. header("Location: details.php?doc=" . urlencode($documentNumber));
  74. exit();
  75. }
  76. ?>
  77. <!DOCTYPE html>
  78. <html lang="pl">
  79. <head>
  80. <meta charset="UTF-8">
  81. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  82. <title>Pozycje dokumentu</title>
  83. <link rel="stylesheet" href="style.css">
  84. <script src="scripts.js" defer></script>
  85. </head>
  86. <body>
  87. <div class="container">
  88. <div class="back-button">
  89. <button onclick="window.location.href='index.php?date=<?php echo htmlspecialchars($operationDate); ?>'">Powrót</button>
  90. </div>
  91.  
  92. <h2>Pozycja <?php echo $currentPosition + 1; ?>/<?php echo $totalPositions; ?> : <?php echo htmlspecialchars($documentNumber); ?></h2>
  93.  
  94. <div id="searchContainer">
  95. <input type="text" id="searchField" placeholder="Wyszukaj EAN lub KODTW" onkeydown="checkEnter(event)">
  96. </div>
  97.  
  98. <form method="POST" action="details.php?doc=<?php echo urlencode($documentNumber); ?>" id="detailsForm">
  99. <table id="dataTable">
  100. <?php
  101. $EAN = htmlspecialchars($currentRow['EAN'] ?? '');
  102. $productShortcut = htmlspecialchars($currentRow['KODTW'] ?? '');
  103. $description = htmlspecialchars($currentRow['OPISTW'] ?? '');
  104. $quantity = floatval($currentRow['QUANTITY'] ?? 0);
  105.  
  106. $sqlCheck = "SELECT check_value FROM wz_records WHERE document_number = ? AND position_number = ?";
  107. $stmtCheck = $mysqli->prepare($sqlCheck);
  108. $stmtCheck->bind_param("si", $documentNumber, $positionNumber);
  109. $stmtCheck->execute();
  110. $stmtCheck->bind_result($savedCheckValue);
  111. $stmtCheck->fetch();
  112. $stmtCheck->close();
  113.  
  114. $checkValue = floatval($savedCheckValue ?? '');
  115. $isDisabled = ($checkValue !== '' && $checkValue == $quantity) ? 'disabled' : '';
  116. $rowClass = ($checkValue !== '' && $checkValue == $quantity) ? 'success' : ($checkValue !== '' ? 'error' : '');
  117.  
  118. echo "<tr class='$rowClass'>";
  119. echo "<th>EAN</th><td>$EAN</td>";
  120. echo "</tr>";
  121. echo "<tr class='$rowClass'>";
  122. echo "<th>KOD TW</th><td>$productShortcut</td>";
  123. echo "</tr>";
  124. echo "<tr class='$rowClass'>";
  125. echo "<th>OPIS</th><td>$description</td>";
  126. echo "</tr>";
  127. echo "<tr class='$rowClass'>";
  128. echo "<th>ILOŚĆ</th><td>$quantity</td>";
  129. echo "</tr>";
  130. echo "<tr class='$rowClass'>";
  131. echo "<th>CHECK</th><td><input type='number' name='check[$currentPosition]' value='' $isDisabled required></td>";
  132. echo "</tr>";
  133. echo "<input type='hidden' name='quantity[$currentPosition]' value='$quantity'>";
  134. echo "<input type='hidden' name='productShortcut[$currentPosition]' value='$productShortcut'>";
  135. echo "<input type='hidden' name='description[$currentPosition]' value='$description'>";
  136. ?>
  137. </table>
  138. <input type="submit" name="save" value="Save">
  139. </form>
  140. </div>
  141. </body>
  142. </html>




[JAVASCRIPT] pobierz, plaintext
  1. function checkEnter(event) {
  2. if (event.key === "Enter") {
  3. event.preventDefault();
  4. let searchValue = document.getElementById('searchField').value.trim().toUpperCase();
  5. if (searchValue !== '') {
  6. let rows = document.querySelectorAll('#dataTable tr');
  7. let found = false;
  8. let firstMatch = null;
  9.  
  10. rows.forEach(row => {
  11. let ean = row.getAttribute('data-ean') ? row.getAttribute('data-ean').toUpperCase() : '';
  12. let kodtw = row.getAttribute('data-kodtw') ? row.getAttribute('data-kodtw').toUpperCase() : '';
  13.  
  14. if (ean.includes(searchValue) || kodtw.includes(searchValue)) {
  15. row.classList.add('highlight');
  16. if (!found) {
  17.  
  18. let checkInput = row.querySelector('input[type="number"]');
  19. if (checkInput) {
  20. checkInput.focus();
  21. found = true;
  22. }
  23. }
  24. } else {
  25. row.classList.remove('highlight');
  26. }
  27. });
  28.  
  29. if (!found) {
  30. alert('Nie znaleziono żadnych pasujących pozycji.');
  31. let searchField = document.getElementById('searchField');
  32. searchField.focus();
  33. searchField.value = '';
  34. }
  35. }
  36. }
  37. }
  38.  
  39. document.getElementById('detailsForm').addEventListener('keydown', function (event) {
  40. if (event.key === "Enter") {
  41. event.preventDefault();
  42. let activeElement = document.activeElement;
  43. if (activeElement.tagName.toLowerCase() === 'input' && activeElement.type === 'number') {
  44. handleEnter(event, activeElement);
  45. }
  46. }
  47. });
  48.  
  49. function handleEnter(event, element) {
  50. let row = element.closest('tr');
  51. let quantity = parseFloat(row.querySelector('input[name^="quantity"]').value);
  52. let checkValue = parseFloat(element.value);
  53.  
  54. if (checkValue === quantity) {
  55. row.classList.remove('error');
  56. row.classList.add('success');
  57. element.setAttribute('readonly', true);
  58. goToNextPosition();
  59. } else {
  60. row.classList.remove('success');
  61. row.classList.add('error');
  62. alert('Ilość nie zgadza się! Popraw dane.');
  63. }
  64. }
  65.  
  66. function goToNextPosition() {
  67. let rows = document.querySelectorAll('#dataTable tr');
  68. let currentRow = Array.from(rows).find(row => row.classList.contains('success'));
  69.  
  70. if (currentRow) {
  71. let nextRow = currentRow.nextElementSibling;
  72. if (nextRow) {
  73. let nextInput = nextRow.querySelector('input[name^="check"]');
  74. if (nextInput) {
  75. nextInput.focus();
  76. }
  77. } else {
  78. alert('Osiągnięto ostatnią pozycję.');
  79. }
  80. }
  81. }
[JAVASCRIPT] pobierz, plaintext