Z
Z
Zina-20002021-06-13 11:17:31
PHP
Zina-2000, 2021-06-13 11:17:31

Why aren't select values ​​loaded?

Hello! I'm making linked lists with data from the database. Why are the values ​​in the second select not loaded after the selection in the first? I do video.

Two tables are used from the database:
1) languages_ with id and language columns
2) countries_ with id, id_language and country columns

Here is the select code:

<?php
try{
$db = new PDO ("mysql: dbname=lesko;host=localhost","root", "", array(
  PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8",
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
  PDO::ATTR_ERRMODE => TRUE
));
}catch (PDOExeception $error){
  die($error->getMessage() );
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>Связные списки</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>  
  <script>
    $(function(){
      var id = $(".language").val ();
      $.ajax ({
        type: "POST",
        url: "country.php",
        data: {id: id}, 
        success: function(data) {
          $(".country").html (data) ;
        }
      });
      $(".language").change(function(){
        var id = $(".language").val ();
        if(id == 0) {

        }
        $.ajax ({
          type: "POST",
          url: "country.php",
          data: {id: id},
          success: function(data) {
            $(".country").html (data) ;
          }
        });
      });
    });
  </script>

    <select class="language">
        <option value="0">выберете язык</option>
      <?php
        $query = $db->query("SELECT * FROM lesko.languages_ ");
        while($row = $query->fetch() ){
          echo "<option value='($row->id)'>".$row->language."</option>";
        }
       ?>
    </select>

<span class="country">
  
</span>

</body>
</html>


Here, in theory, a selection of countries should be performed (country.php):
<?php
  try{
  $db = new PDO ("mysql: dbname=lesko;host=localhost","root", "", array(
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8",
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
    PDO::ATTR_ERRMODE => TRUE
  ));
  }catch (PDOExeception $error){
    die($error->getMessage() );
  }
?>

<?
if (isset($_POST['id']) && !empty($_POST['id'])){
  $id = intval($_POST['id']);
  $query = $db->query("SELECT * FROM lesko.countries_ WHERE id_language=$id");
  echo "<select name='country'>";
  while ($row = $query->fetch() ){
    echo "<option>{$row->country}</option>";
  }
  echo "</select>";
}else{
  echo "<select name='country' disabled><option value='0'>выберете страну</option></select>";
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Edward, 2021-06-16
@Drayde

$query = $db->query("SELECT * FROM lesko.countries_ WHERE id_language=$id");

Replace with
$query = $db->query("SELECT * FROM lesko.countries_ WHERE id_language={$id}");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question