From d76f910f398616ef76c51d42515ab64f2d8c01f7 Mon Sep 17 00:00:00 2001 From: aguzik Date: Tue, 15 Oct 2024 13:01:00 +0000 Subject: [PATCH] Upload files to "/" --- index.html | 19 +++++++++++++++++ php.php | 44 +++++++++++++++++++++++++++++++++++++++ script.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 index.html create mode 100644 php.php create mode 100644 script.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..060957e --- /dev/null +++ b/index.html @@ -0,0 +1,19 @@ + + + + + + Document + + + + + + +
+ + + + \ No newline at end of file diff --git a/php.php b/php.php new file mode 100644 index 0000000..fe58108 --- /dev/null +++ b/php.php @@ -0,0 +1,44 @@ +'wyslane']); + } else { + echo json_encode(['status'=>'blad']); + } + } + + + if($_SERVER['REQUEST_METHOD'] == "GET" && isset($_GET['type'])){ + $ans = mysqli_query($con, "SELECT * FROM zdj"); + $json = array(); + while($row = mysqli_fetch_assoc($ans)){ + $json[] = $row; + } + echo json_encode($json); + } + + + + if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET["id"])){ + $ans = mysqli_query($con, "SELECT zdj FROM zdj WHERE id = ". $_GET["id"]); + $json = array(); + if($ans){ + while($row = mysqli_fetch_assoc($ans)){ + $json[] = $row; + } + echo json_encode($json); + } else { + echo json_encode(["status"=>"nie ma danych"]); + } + } + +?> \ No newline at end of file diff --git a/script.js b/script.js new file mode 100644 index 0000000..51bd896 --- /dev/null +++ b/script.js @@ -0,0 +1,60 @@ +document.addEventListener('DOMContentLoaded', async ()=>{ + const button = document.getElementById('submitZdj') + const selection = document.getElementById("wybor") + const img = document.getElementById('img') + + let daneJson = await fetch('php.php?type=all') + if(!daneJson.ok){ + throw new Error("Błąd połączenia") + } + let dane = await daneJson.json() + console.log(dane) + dane.forEach(opcja=>{ + const option = document.createElement("option") + option.value = opcja.id + option.textContent = opcja.nazwa + selection.appendChild(option) + }) + + async function getBase64(file) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result); + reader.onerror = error => reject(error); + }); + } + + selection.addEventListener('change', async (event)=>{ + let ansJson = await fetch('php.php?id=' + event.target.value) + let ans = await ansJson.json() + img.src = ans[0].zdj + console.log(ans) + }) + + button.addEventListener('click', async () =>{ + const zdjInput = document.getElementById("zdjecie") + + const zdj = zdjInput.files[0] + const data = { + zdj: await getBase64(zdj), + nazwa: zdj.name + } + console.log(data) + + let fetchans = await fetch('php.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }) + + if(!fetchans.ok){ + throw new Error("Bład połączenia") + } + let ans = await fetchans.json() + console.log(ans) + }) + +}) \ No newline at end of file