
Mam problem z działaniem potrójnego selecta z lokalizacją (klasyczne country, state, city).
index.php
<select id="countrydd" onChange="changeCountry()"> <option value="0">Select Country</option> <?php $countries_count = $connect->query("SELECT * FROM countries"); while($cc = $countries_count->fetch_assoc()) { } ?> </select> <div id="state"> <select> <option>Select State</option> </select> </div> <div id="city"> <select> <option>Select City</option> </select> </div> <script type="text/javascript"> function changeCountry() { var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "ajax.php?country="+$("#countrydd").val(), false); xmlhttp.send(null); $("#state").html(xmlhttp.responseText); } function changeState() { var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "ajax.php?state="+$("#statedd").val(), false); xmlhttp.send(null); $("#city").html(xmlhttp.responseText); } </script>
ajax.php
<?php require_once "connection.php"; mysqli_report(MYSQLI_REPORT_STRICT); $connect = new mysqli($host, $db_user, $db_password, $db_name); $country = $_GET["country"]; $state = $_GET["state"]; { $states_count = $connect->query("SELECT * FROM states WHERE country_id='$country'"); if($states_count->num_rows > 0) { echo '<select id="statedd" onChange="changeState()"> <option>Select State</option>'; while($sc = $states_count->fetch_assoc()) { } } else { } } { $cities_count = $connect->query("SELECT * FROM cities WHERE state_id='$state'"); if($cities_count->num_rows > 0) { echo '<select> <option>Select City</option>'; while($cc = $cities_count->fetch_assoc()) { } } else { } } ?>
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:
Notice: Undefined index: state in C:\xampp\htdocs\test\ajax.php on line 8 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
