source: backend/GlobeGuru-backend/frontend/js/formHandler.js@ d4d8f61

Last change on this file since d4d8f61 was d4d8f61, checked in by Kikac324 <kristijanzafirovski26@…>, 7 months ago

init

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[d4d8f61]1document.addEventListener('DOMContentLoaded', function() {
2 const form = document.getElementById('form-id'); // Ensure the form has the correct ID
3
4 form.addEventListener('submit', function(event) {
5 event.preventDefault(); // Prevent the default form submission
6
7 // Collect form data
8 const formData = new FormData(form);
9 const data = {};
10 formData.forEach((value, key) => {
11 data[key] = value;
12 });
13
14 // Send form data to the server
15 fetch('/submit', {
16 method: 'POST',
17 headers: {
18 'Content-Type': 'application/json'
19 },
20 body: JSON.stringify(data)
21 })
22 .then(response => response.json())
23 .then(data => {
24 // Process the scraped data and update the page
25 const dataList = document.getElementById('givenOptions');
26 dataList.innerHTML = ''; // Clear previous data
27
28 data.forEach(item => {
29 // Create option container div
30 const optionDiv = document.createElement('div');
31 optionDiv.classList.add('option');
32
33 // Create and append image element
34 const img = document.createElement('img');
35 img.classList.add('image');
36 img.src = item.imgSrc; // Use item.imageSrc if available
37 optionDiv.appendChild(img);
38
39 // Create and append hotel name paragraph
40 const nameParagraph = document.createElement('p');
41 nameParagraph.id = 'name';
42 nameParagraph.textContent = item.hotelName;
43 optionDiv.appendChild(nameParagraph);
44
45 // Create and append country paragraph
46 const countryParagraph = document.createElement('p');
47 countryParagraph.id = 'country';
48 countryParagraph.style.fontSize = '20px';
49 countryParagraph.textContent = item.country;
50 optionDiv.appendChild(countryParagraph);
51
52 // Create and append price heading and paragraph
53 const priceHeading = document.createElement('h1');
54 priceHeading.textContent = 'Цена:';
55 optionDiv.appendChild(priceHeading);
56 const priceParagraph = document.createElement('h2');
57 priceParagraph.id = 'price';
58 priceParagraph.textContent = item.price;
59 optionDiv.appendChild(priceParagraph);
60
61 // Create link and button
62 const link = document.createElement('a');
63 link.id = 'link';
64 link.href = item.link; // Use item.link if available
65 link.target = '_blank'; // Open link in new tab
66
67 const button = document.createElement('button');
68 button.classList.add('btn');
69 button.textContent = 'Линк до страна';
70
71 link.appendChild(button);
72 optionDiv.appendChild(link);
73
74 // Append option div to dataList
75 dataList.appendChild(optionDiv);
76 });
77 })
78 .catch(error => {
79 console.error('Error fetching data:', error);
80 });
81 });
82});
Note: See TracBrowser for help on using the repository browser.