M
M
mr_rakhimov2021-10-28 15:40:19
AJAX
mr_rakhimov, 2021-10-28 15:40:19

How to pass $_GET parameter via AJAX to PHP handler?

I prepared the code for dependent lists. There are only 3 lists. When you select a value in the first list, the content in the second one changes (the selection comes from the database); selecting a value in the second list changes the content in the third. So, I pass the values ​​from the lists to the php handler via an ajax request, which assigns the value to the $_GET superarray in the corresponding key. With ajax code, everything is in order, it passes everything to the handler, as I request. The problem comes in the handler itself. Namely, when the third list should change its contents relative to the second one, the handler cannot correctly fetch from the database, because the handler did not receive the $_GET parameter from the first list (from the first fetch). How can I pass the selected $_GET from the first list to the handler of the second?

*I use PDO RedBeanPhP, you can ignore the selection (everything is correct there)
Code itself:

HTML & JS

<?
require "db.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <script>

  $(document).ready(function(){

    $('#select_worktype').on('change', function(){
      var worktype = $(this).val();

      if(worktype){
        $.get(
          "ajax.php",
          {worktype: worktype},
          function (data){
            $('#select_workspace').html(data);
          }
        );
      }else{
        $('#select_workspace').html('<option disabled selected>Выбор формирования</option>');
      }
    });

    $('#select_workspace').on('change', function(){
      var workspace = $(this).val();

      if(workspace){
        $.get(
          "ajax.php",
          {workspace: workspace},
          function (data2){
            $('#select_position').html(data2);
          }
        );
      }else{
        $('#select_position').html('<option disabled selected>Выбор формирования</option>');
      }
    });

  });

  </script>
</head>
<body>
    <br>
    <br>
    <br>
    <br>
  <form method="POST">
    Выбор типа формирования
    <select name="select_worktype" id="select_worktype">
      <option disabled selected>Выбор типа формирования</option>
      <option value="chairs">Кафедра</option>
      <option value="departments">Отдел</option>
      <option value="managements">Руководство</option>
      <option value="centers">Центр</option>
      <option value="controls">Управление</option>
      <option value="groups">Группа</option>
      <option value="others">Иное</option>
    </select>

    <br>
    <br>
    <br>
    <br>

    Выбор формирования
    <select name="select_workspace" id="select_workspace">
      <option disabled selected>Выбор формирования</option>
    </select>

    <br>
    <br>
    <br>
    <br>

    Выбор должности
    <select name="select_position" id="select_position">
      <option disabled selected>Выбор должности</option>
    </select>
  </form>
</body>
</html>


PHP (ajax.php file)
<?
require "db.php";

if(isset($_GET['worktype']) && !empty($_GET['worktype'])){
  echo '<option disabled selected>Выбор формирования</option>';

$worktype=$_GET['worktype'];

$last_worktype = R::findLast(''.$worktype.'');
$last_id=$last_worktype->id;

for($i=1; $i!=$last_id+1 ; $i++){
  $search_workspace =R::findOne(''.$worktype.'',  'id = ?', array($i));
  $search_workspace_name=$search_workspace->name;
  $search_workspace_status=$search_workspace->status;

  if($search_workspace_status==1){
    echo '<option value="'.$i.'">'.$search_workspace_name.'</option>';
  }

}

}else{

}





if(isset($_GET['workspace']) && !empty($_GET['workspace'])){
  echo '<option disabled selected>Выбор должности</option>';

  $workspace=$_GET['workspace'];

  $view_position=R::findOne(''.$worktype.'',  'id = ?', array($workspace));
  $view_position_positions=$view_position->positions;
  $view_position_status=$view_position->status;

  $this_positions=unserialize($view_position_positions);

  foreach($this_positions as $key=>$value){
    if(!empty($value)){
      echo '<option value="'.$key.'">'.$value.'</option>';
    }
  }

}else{

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-10-28
@mr_rakhimov

$('#select_workspace').on('change', function(){
      var workspace = $(this).val();
      // ***********************************************
      var worktype = $('#select_worktype').val();
      // ***********************************************

      if(workspace){
        $.get(
          "ajax.php",
          {workspace: workspace, worktype:worktype},
          function (data2){
            $('#select_position').html(data2);
          }
        );
      }else{
        $('#select_position').html('<option disabled selected>Выбор формирования</option>');
      }
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question