vendredi 26 mars 2021

Can i show cities and districts in the dropdown menu with php using Ajax?

I have a web page with php extension and the data information I want to get from the database with php. Normally, I can display my cities information in the dropdown menu on my website, but when I select that city, I want it to bring the districts of that cities in the menu below, but that part does not work.

select.php

 <html>
<head>
<link rel="stylesheet" type="text/css" href="select_style.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function fetch_select(val)
{
 $.ajax({
 type: 'get',
 url: 'fetch_data.php',
 data: {
 ilid:val
 },
 success: function (response) {
  document.getElementById("new_select").innerHTML=response; 
 }
 });
}

</script>

</head>
<body>
<p id="heading">Dynamic Select Option Menu Using Ajax and PHP</p>
<center>
<div id="select_box">
 <select onchange="fetch_select(this.value);">
  <option>Select City</option>
  <?php

 $db = pg_connect("host='localhost' port='5432' dbname='test' user='postgres' password='***'");



 $query = "SELECT ilid,iladi FROM il";

 $result = pg_query($query);
 if (!$result) {
     echo "Problem with query " . $query . "<br/>";
     echo pg_last_error();
     exit();
 }
 while($myrow = pg_fetch_assoc($result)) {
     printf ("<option value='".$myrow['ilid']."'>".$myrow['iladi']."</option>");
 }
 ?>
 </select>

 <select id="new_select">
 </select>
      
</div>     
</center>
</body>
</html>

fetch_data.php

<?php
$db = pg_connect("host='localhost' port='5432' dbname='test' user='postgres' password='***'");

$selectedil = $_GET['ilid'];

$query = "SELECT ilceid,ilceadi FROM ilce where ilid = '{$selectedil}'";

$result = pg_query($query);
if (!$result) {
    echo "Problem with query " . $query . "<br/>";
    echo pg_last_error();
    exit();
}
while($myrow = pg_fetch_assoc($result)) {
    printf ("<option value='".$myrow['ilceid']."'>".$myrow['ilceadi']."</option>");
}

?>

My cities come here, but when I click on these cities, the districts do not appear in the menu below.

Here, too, I can call the districts with my other php page. When you type ilid = 10, the districts of the 10th city appear on the screen.

I cannot show it on my website. Where am I making a mistake?




Aucun commentaire:

Enregistrer un commentaire