Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dropdown z lokalizacją
aerospeed
post
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 8.10.2017

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


Witam szanowne grono forumowiczów (IMG:style_emoticons/default/smile.gif)

Mam problem z działaniem potrójnego selecta z lokalizacją (klasyczne country, state, city).

index.php
  1. <select id="countrydd" onChange="changeCountry()">
  2. <option value="0">Select Country</option>
  3. <?php
  4. $countries_count = $connect->query("SELECT * FROM countries");
  5. while($cc = $countries_count->fetch_assoc())
  6. {
  7. echo '<option value="'.$cc['id'].'">'.$cc['name'].'</option>';
  8. }
  9. ?>
  10. </select>
  11.  
  12. <div id="state">
  13. <select>
  14. <option>Select State</option>
  15. </select>
  16. </div>
  17.  
  18. <div id="city">
  19. <select>
  20. <option>Select City</option>
  21. </select>
  22. </div>
  23.  
  24. <script type="text/javascript">
  25. function changeCountry()
  26. {
  27. var xmlhttp = new XMLHttpRequest();
  28. xmlhttp.open("GET", "ajax.php?country="+$("#countrydd").val(), false);
  29. xmlhttp.send(null);
  30. $("#state").html(xmlhttp.responseText);
  31. }
  32.  
  33. function changeState()
  34. {
  35. var xmlhttp = new XMLHttpRequest();
  36. xmlhttp.open("GET", "ajax.php?state="+$("#statedd").val(), false);
  37. xmlhttp.send(null);
  38. $("#city").html(xmlhttp.responseText);
  39. }
  40.  
  41. </script>


ajax.php
  1. <?php
  2.  
  3. require_once "connection.php";
  4. mysqli_report(MYSQLI_REPORT_STRICT);
  5.  
  6. $connect = new mysqli($host, $db_user, $db_password, $db_name);
  7. $country = $_GET["country"];
  8. $state = $_GET["state"];
  9.  
  10. if(isset ($country))
  11. {
  12.  
  13. $states_count = $connect->query("SELECT * FROM states WHERE country_id='$country'");
  14.  
  15. if($states_count->num_rows > 0)
  16. {
  17. echo '<select id="statedd" onChange="changeState()">
  18. <option>Select State</option>';
  19.  
  20. while($sc = $states_count->fetch_assoc())
  21. {
  22. echo '<option value="'.$sc['id'].'">'.$sc['name'].'</option>';
  23. }
  24.  
  25. echo '</select>';
  26. }
  27. else
  28. {
  29. echo '<select><option>Select State</option></select>';
  30. }
  31.  
  32. }
  33.  
  34. if(isset($state))
  35. {
  36.  
  37. $cities_count = $connect->query("SELECT * FROM cities WHERE state_id='$state'");
  38.  
  39. if($cities_count->num_rows > 0)
  40. {
  41. echo '<select>
  42. <option>Select City</option>';
  43.  
  44. while($cc = $cities_count->fetch_assoc())
  45. {
  46. echo '<option value="'.$cc['id'].'">'.$cc['name'].'</option>';
  47. }
  48.  
  49. echo '</select>';
  50. }
  51. else
  52. {
  53. echo '<select><option>Select City</option></select>';
  54. }
  55.  
  56. }
  57.  
  58. ?>


Teraz, problem w tym, że pomimo iż skrypt działa, bo wyświetla prawidłowe wyniki z bazy danych, to z jakiegoś powodu php wyrzuca na ekran błędy:

  1. Notice: Undefined index: state in C:\xampp\htdocs\test\ajax.php on line 8
  2. Notice: Undefined index: country in C:\xampp\htdocs\test\ajax.php on line 7


Próbowałem już na wszystkie znane mi sposoby to naprawić i nadal nie wiem w czym problem.
Z góry dzięki za pomoc.
Pozdrawiam (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 23.12.2025 - 13:13