I just can’t make friends with jQuery :)
Hello, there are problems with change
. There is a form with a selector city and district. Task: when a user selects a city in the selector, the next asynchronous function should be executed via ajax
and php
("Pull out all districts where the area is City = 'city'"), here’s the ajax
request:
$(document).ready(function(){ $("#city").change(function(){var myChoice = $('#city :selected').val(); var data = {myChoice: myChoice} $.ajax({ type: "POST", url: "../blocks/change.php", data: data, dataType: 'json', success: function(data){ var top_list = data.myrowRaion; // содержимое новых элементов var items = "<option>"; // вставляемый html-текст $mylist = $('#raion'); // необходимый список for (var i=0; i< top_list.length; i++) items += top_list[i] + '</option>'; $mylist.append(items); }}); }); });
and here is the PHP code:
<?php include("../blocks/bd.php"); if (isset($_POST['myChoice'])) { $myChoice = $_POST['myChoice']; } if (isset($myChoice)) { $resultRaion = mysql_query("SELECT raion, id FROM raion WHERE idCity = '$myChoice' ", $db); if (!$resultRaion) { echo "<p>Отправьте код на почту san-goldencity@mail.ru:</p>"; exit(mysql_error()); } if (mysql_num_rows($resultRaion) > 0) { $myrowRaion = mysql_fetch_array($resultRaion); } else { echo "<p>Таблицы еще пустые, записей отствует</p>"; exit(); } } $myrowRaion[] = $myrowRaion; echo json_encode($myrowRaion); ?>
here is the code form:
<select name = "city" id = "city"> <? $ resultCity = mysql_query ("SELECT city, id FROM city", $ db); if (! $ resultCity) {echo "<p> Send an error code to this mail san-goldencity@mail.ru: </ p>"; exit (mysql_error ());}
if (mysql_num_rows($resultCity) > 0) { $myrowCity = mysql_fetch_array($resultCity); do { printf ("<option value='%s'>%s</option>",$myrowCity["id"], $myrowCity["city"]); } while ($myrowCity = mysql_fetch_array($resultCity)); } else { echo "<p>Таблицы еще пустые, записей отствует</p>"; exit (); } ?> </select> <p class="formP">Район:</p> <select name="raion" id="raion"> <option>Кутузовка</option> <option>Заречка</option> <option>15мкрн</option> <option>новый город</option> </select>
Help, what am I doing wrong, tell me?