Good afternoon, tell the newcomer. Trying to implement dynamic select ec

/*я понимаю что данные посылаются в какойто php файл и там идёт выборка из базы и возврат данных ктр подставятся на место <select id="user-list" >*/ function getIdCompany(val) { $.ajax({ type: "POST", url: "#", data: { idCompany: val }, success: function(data) { $("#user-list").html(data); } }); } 
 <select id="company-list" onchange="getIdCompany(this.value)"> <option value="1">Компания 1</option> <option value="2">Компания 2</option> <option value="3">Компания 3</option> <option value="4">Компания 4</option> </select> <!--Сюда потом подгрузим выборку из базы--> <select id="user-list"> <option value="0">-Выберите пользователя-</option> </select> <!--хочу проверить что приходит в $_POST, а там пусто.--> <?php var_dump($_POST); ?> 

  • post-request to another file take out for example post.php - Jean-Claude

1 answer 1

I do not know about such a possibility to make a request to a file on myself, as you will receive from yourself, if html-content is entered there, then you have received not only a variable, but also the entire html (theoretically).

This is how it works, the index.php file

 <!DOCTYPE html> <html> <head> <title>titile</title> <meta charset="utf-8"> <style> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function(){ $('#company-list').on('change', function(event) { console.log($(this).val()); $.ajax({ url: 'post.php', type: 'POST', dataType: 'html', data: {idCompany: $(this).val()}, }) .done(function() { console.log("success"); }) .fail(function() { console.log("error"); }); }); }); </script> </head> <body> <select id="company-list"> <option value="1">Компания 1</option> <option value="2">Компания 2</option> <option value="3">Компания 3</option> <option value="4">Компания 4</option> </select> </body> </html> 

post.php

 <?php var_dump($_POST); ?> 
  • Good afternoon, thanks for the reply, I figured it out myself. You can only watch through the console. And the data does fall into $ _POST. but var_dump will not work even if put into a separate file. The page is not updated. - PeP1L
  • @ PeP1L for Ajax does not need to refresh the page. - Jean-Claude