Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript][MySQL][PHP]Cookie z wyborem w select i pobieranie danych z MYSQL
brzanek
post
Post #1





Grupa: Zarejestrowani
Postów: 429
Pomógł: 0
Dołączył: 8.11.2012

Ostrzeżenie: (0%)
-----


Witam w bazie danych mysql mam dane o odpowiednich id.
Zanim je pobiorę chcę aby po wejściu na stronę w osobnym oknie pojawił się mały formularz z wyborem select. Wybór ten zapisuje się w ciasteczku i pamięta to przez np. rok. Wybór w formularzu powodował by pobieranie odpowiednich danych o jakimś id z bazy danych.
Mam nadzieję, że jakoś to wytłumaczyłem.
Jak narazie mam coś takiego
  1. <?
  2.  
  3. ?>
  4. <select id="ThemeSelect" onchange="saveTheme(this.value);">
  5. <option value="1">CHOSZCZNO</option>
  6. <option value="2">SZCZECIN</option>
  7. <option value="3">KOSZALIN</option>
  8. </select>
  9. <script>
  10. var saveclass = null;
  11.  
  12. function saveTheme(cookieValue)
  13. {
  14. var sel = document.getElementById('ThemeSelect');
  15.  
  16. saveclass = saveclass ? saveclass : document.body.className;
  17. document.body.className = saveclass + ' ' + sel.value;
  18.  
  19. setCookie('theme', cookieValue, 365);
  20. }
  21.  
  22. function setCookie(cookieName, cookieValue, nDays) {
  23. var today = new Date();
  24. var expire = new Date();
  25.  
  26. if (nDays==null || nDays==0)
  27. nDays=1;
  28.  
  29. expire.setTime(today.getTime() + 3600000*24*nDays);
  30. document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
  31. }
  32.  
  33. function readCookie(name) {
  34. var nameEQ = name + "=";
  35. var ca = document.cookie.split(';');
  36. for(var i = 0; i < ca.length; i++) {
  37. var c = ca[i];
  38. while (c.charAt(0) == ' ') c = c.substring(1, c.length);
  39. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  40. }
  41. return null;
  42. }
  43.  
  44. document.addEventListener('DOMContentLoaded', function() {
  45. var themeSelect = document.getElementById('ThemeSelect');
  46. var selectedTheme = readCookie('theme');
  47.  
  48. if (selectedTheme) {
  49. themeSelect.value = selectedTheme;
  50. }
  51. });
  52. </script>
  53. <?
  54. ?>

Po wyborze ciasteczko zapisuje się ale jak to teraz połączyć z bazą danych.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 07:06