N
N
Nikita2019-05-26 15:34:03
AJAX
Nikita, 2019-05-26 15:34:03

Why is data not being received using AJAX?

There is a page on which all data from the column (person's last name) is displayed from MYSQL and upon clicking, full information about the person should be displayed (that is, after clicking, processing takes place in js and the person's last name is transmitted via ajax to php in order to subsequently make a request to the database) .
5cea87e63d3e3404651889.png
I checked everything I could, I came to the conclusion that $select_value is empty, then there is no data transfer with AJAX, although the success function is executed:
5cea86de73556993284022.png
Now it goes from viewing.php to viewing.php, I know that this is not very good.
I tried to transfer to another file and connect it, the variable is still empty.
Please tell me what is wrong, I can not understand the reason: why $select_value is empty
viewing.php:

<!DOCTYPE html>
<html lang="ru">
<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>
  <link rel="icon" href="img/favicon.png" type="image/png">
  <link rel="stylesheet" href="css/normalize.css">
  <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
  <link rel="stylesheet" href="css/styles.css">

</head>
<body>
<?php include('header.php'); 
require_once 'php/mysql_connect.php';
?>
<div class="viewing-wrapper">
  <div class="viewing-container">
    <div class="viewing-title">
      Выберите абоненета из списка
    </div>
    <div class="viewing-list">
      <div class="viewing-select-wrap">
      <select class="viewing-select">
      <?php 
  $query = $pdo->query('SELECT * FROM `telephone` ORDER BY `fullname`');
  while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo  '<option>'.$row['fullname'] . '</option>';
  } ?>
      </select>
      <button class="button btn-show">Показать</button>
      </div>
    </div>
    <div class="viewing-detailed-output">
    <?php 
  $select_value = trim($_POST['select_value']);
     $select_value = 'Суркаев'; 
        $query = $pdo->prepare('SELECT * FROM `telephone` WHERE `fullname` LIKE  ? ');
      $query->execute([$select_value]); 
      while($row = $query->fetch(PDO::FETCH_ASSOC)) {
        echo ' ' . $row['id'] . ' ' . $row['fullname'] . ' ' . $row['phone'] . ' ' .  $row['street'] . ' '. $row['housenumber'] . '<br>';
      }
    ?>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
 const viewingSelect = document.querySelector(".viewing-select"),
       btnShow = document.querySelector(".btn-show");
 
 $(document).ready(function () {
function sendToPhp () {
  btnShow.addEventListener("click", () => {
  let select_value = viewingSelect.value;
    console.log(select_value);
    $.ajax({
      url: "viewing.php",
      type: "POST",
      cache: false,
      data: {
        ajax: 'yes',
        select_value: select_value
      },
      success: function () {
        console.log(`Переменная ${select_value} отправлена`);
      }
    });
  });
}
sendToPhp();
});
</script>
<?php 
?>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question