inf03-06.2022-z2/js/get.js

36 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-11-08 11:41:12 +00:00
document.addEventListener('DOMContentLoaded', function() {
const imieSelect = document.getElementById('imie');
const photoDisplay = document.createElement('img');
photoDisplay.style.width = "200px";
photoDisplay.style.height = "auto";
document.getElementById('prawy').appendChild(photoDisplay);
let usersData = [];
imieSelect.addEventListener('focus', function() {
fetch('get.php')
.then(response => response.json())
.then(data => {
usersData = data;
imieSelect.innerHTML = "";
data.forEach(dane => {
const option = document.createElement('option');
option.value = dane.id;
option.textContent = dane.imie + " " + dane.nazwisko;
imieSelect.appendChild(option);
});
})
.catch(error => console.error('Error fetching data:', error));
});
imieSelect.addEventListener('change', function() {
const selectedId = imieSelect.value;
const selectedPerson = usersData.find(dane => dane.id == selectedId);
if (selectedPerson && selectedPerson.photo) {
photoDisplay.src = 'data:image/jpeg;base64,' + selectedPerson.photo;
} else {
photoDisplay.src = '';
}
});
});