diff --git a/login/login.js b/login/login.js index e3f5856..10dea29 100644 --- a/login/login.js +++ b/login/login.js @@ -15,8 +15,7 @@ document.addEventListener('DOMContentLoaded', function () { const result = await response.json(); output.innerHTML = result.msg; output.style.display = "block"; - setTimeout(function () { if (result.redirect) window.location.replace(result.redirect); }, 300); - + if (result.redirect) setTimeout(function () { window.location.replace(result.redirect); }, 300); }); document.getElementById("guest").addEventListener("click", async function () { const formData = new FormData(); @@ -32,7 +31,7 @@ document.addEventListener('DOMContentLoaded', function () { const result = await response.json(); output.innerHTML = result.msg; output.style.display = "block"; - setTimeout(function () { if (result.redirect) window.location.replace(result.redirect); }, 300); + if (result.redirect) setTimeout(function () { window.location.replace(result.redirect); }, 300); }); passwordI = document.getElementById("password"); document.getElementById("showPassword").addEventListener("mousedown", function () { passwordI.type = "text"; }); diff --git a/login/login.php b/login/login.php index b39da33..0182466 100644 --- a/login/login.php +++ b/login/login.php @@ -22,7 +22,6 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["username"]) && isset($ $conn->close(); } elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guest"])) { $_SESSION["UID"] = 0; - header('Content-Type: application/json'); echo json_encode(["redirect" => "/", "msg" => "Zalogowano jako gość"]); } else jsonMsg("Nieprawidłowy request"); diff --git a/login/register/register.js b/login/register/register.js index 4b56335..50c0bcb 100644 --- a/login/register/register.js +++ b/login/register/register.js @@ -19,7 +19,7 @@ document.addEventListener("DOMContentLoaded", function () { }); const result = await response.json(); msg(result.msg); - setTimeout(function () { if (result.redirect) window.location.replace(result.redirect); }, 300); + if (result.redirect) setTimeout(function () { window.location.replace(result.redirect); }, 300); } } else msg("Hasła nie są identyczne"); }); diff --git a/login/register/register.php b/login/register/register.php index 410c9c0..b3fbef9 100644 --- a/login/register/register.php +++ b/login/register/register.php @@ -23,7 +23,6 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["username"]) && isset($ $stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt->bind_param("ss", $username, $password); if ($stmt->execute()) { - header('Content-Type: application/json'); echo json_encode(["redirect" => "/login/", "msg" => "Utworzono użytkownika $username"]); } else jsonMsg("Nie udało się utworzyć użytkownika"); diff --git a/php/functions.php b/php/functions.php index e006935..6d0bfd2 100644 --- a/php/functions.php +++ b/php/functions.php @@ -10,7 +10,6 @@ function connectDB() { return $conn; } function jsonMsg($messsage) { - header('Content-Type: application/json'); echo json_encode(["msg" => $messsage]); exit(); } diff --git a/php/scripts.php b/php/scripts.php index 7baf191..1b4b543 100644 --- a/php/scripts.php +++ b/php/scripts.php @@ -4,3 +4,5 @@ if (!isset($_SERVER["HTTP_X_REQUESTED_WITH"]) || strtolower($_SERVER["HTTP_X_REQ http_response_code(403); exit('Odmowa dostępu'); } +header('Content-Type: application/json'); + diff --git a/test/test.js b/test/test.js index 64566e8..0fc9f59 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,6 @@ document.addEventListener("DOMContentLoaded", function () { + const output = document.getElementById("info"); + const mainContent = document.getElementById("content"); document.getElementById("startForm").addEventListener("submit", async function (formE) { formE.preventDefault(); formData = new FormData(formE.target); @@ -11,6 +13,15 @@ document.addEventListener("DOMContentLoaded", function () { } }); const result = await response.json(); - document.getElementById("content").innerHTML = result.msg; + output.innerHTML = result.msg; + output.style.display = "block"; + if (result.questions) setTimeout(function () { + mainContent.innerHTML = ""; + console.log(result.questions); // DEBUG + result.questions.forEach(question => { + mainContent.innerHTML += question.QID; + mainContent.innerHTML += ";"; + }); + }, 300); }); }); \ No newline at end of file diff --git a/test/test.php b/test/test.php index b55448d..ca7d177 100644 --- a/test/test.php +++ b/test/test.php @@ -5,31 +5,28 @@ if (!checkLogin()) { exit(); } if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) { - if ($_POST["testLength"]>100) jsonMsg("Test nie może być dłuższy niż 100 pytań"); + if ($_POST["testLength"] > 100) + jsonMsg("Test nie może być dłuższy niż 100 pytań"); $conn = connectDB(); - $stmt = $conn->prepare("SELECT count(*) FROM questions LIMIT 1"); + $stmt = $conn->prepare("SELECT QID FROM questions"); $stmt->execute(); - $stmt->bind_result($questionsCount); - $stmt->fetch(); - $questionsIDs = []; - {$i = 0; - while ($i != $_POST["testLength"]) { - $randomNum = rand(0, $questionsCount); - if (!in_array($randomNum, $questionsIDs)) { - $questionsIDs[] = $randomNum; - $i++; - } - }} + $stmt->bind_result($QID); + $allQIDs = []; + while ($stmt->fetch()) + $allQIDs[] = $QID; $stmt->close(); + shuffle($allQIDs); + $questionsIDs = array_slice($allQIDs, 0, $_POST["testLength"]); + $filler = implode(",", array_fill(0, count($questionsIDs), "?")); $stmt = $conn->prepare("SELECT QID, content, answerA, answerB, answerC, answerD FROM questions WHERE QID IN ($filler) ORDER BY FIELD(QID, $filler)"); - $types = str_repeat("i", count($questionsIDs)*2); + $types = str_repeat("i", count($questionsIDs) * 2); $arrMerged = array_merge($questionsIDs, $questionsIDs); $stmt->bind_param($types, ...$arrMerged); $stmt->execute(); $stmt->bind_result($QID, $content, $answerA, $answerB, $answerC, $answerD); $result = []; - while ($stmt->fetch()) { + while ($stmt->fetch()) $result["questions"][] = [ "QID" => $QID, "content" => $content, @@ -38,11 +35,9 @@ if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["testLength"])) { "answerC" => $answerC, "answerD" => $answerD ]; - } $stmt->close(); $conn->close(); - $result["msg"] = "Wygenerowano ". count($result["questions"]). " pytań"; - header('Content-Type: application/json'); + $result["msg"] = "Wygenerowano " . count($result["questions"]) . " pytań"; echo json_encode($result); } else jsonMsg("Nieprawidłowy request"); \ No newline at end of file