Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript][AJAX][PHP]Dynamiczny select z użyciem ajax
codeszan
post 26.09.2017, 16:40:55
Post #1





Grupa: Zarejestrowani
Postów: 61
Pomógł: 0
Dołączył: 2.07.2017

Ostrzeżenie: (0%)
-----


Gdy wysyłam formularz dostep do wysłanego pola w CI jest na zasadzie
  1. $this->input->post('menu_id')

Chciałem stworzyć dwa selecty zależne od siebie , cos jak tu:
i prawie mi się to udało, została mi kwestia wywoałania:
  1. $data['pages_list'] = $this->model_pages->showParentPages($this->input->post(menu_id'));

i tu jest pytanie bo nie moge przed wyslaniem formularza miec dostepu do id z pierwszego selecta
tzn. załóżmy, że w selecie pierwszym sa 2 pozycje menu1 i menu2, jak to rozrozniac?
Gdy wywoluje skrypt z palca, tj:
  1. $data['pages_list'] = $this->model_pages->showParentPages(1);

nie ma problemu
Proszę o jakąś rade, poniżej kod:

  1. public function showParentPages($menu_id)
  2. {
  3. $data = array();
  4. foreach (scandir(APPPATH . 'controllers\\frontend\\pages\\') as $file) {
  5. if ($file != '.' && $file != '..') {
  6. $name = explode('.', $file);
  7. //var_dump($name[0]);
  8. $this->db->select('id,slug');
  9. $this->db->from('pages');
  10. $this->db->where('name', $name[0]);
  11. $this->db->where('active', 1);
  12. $this->db->where('group_id', $menu_id);
  13. $query = $this->db->get();
  14. if ($query->num_rows() > 0) {
  15. foreach ($query->result_array() as $row) {
  16. array_push($data, $row);
  17. }
  18.  
  19. }
  20. }
  21. }
  22. return $data;
  23. }


  1. public function test(){
  2.  
  3. $data['pages_list'] = $this->model_pages->showParentPages($this->input->post(menu_id'));
  4. //var_dump($data['pages_list']); exit();
  5.  
  6. if(count($data['pages_list'])){
  7. $pro_select_box = '';
  8. $pro_select_box .= '<option value="0" >No parent</option>';
  9. foreach ($data['pages_list'] as $p){
  10. $pro_select_box .='<option value="'.$p['id'].'" >'.$p['slug'].'</option>';
  11. }
  12. echo json_encode($pro_select_box);
  13.  
}
}

  1. <script>
  2. $(document).ready(function () {
  3. $('#GrouspMenuPage').on('change',function(){
  4. var menu_id = $(this).val();
  5.  
  6. if(menu_id =='')
  7. {
  8. $('#ParentPage').prop( "disabled", true );
  9. }
  10. else
  11. {
  12. $.ajax({
  13. url:"<?php echo base_url(); ?>backend/modules/001/Pages/test",
  14. type:"POST",
  15. data:{'menu_id':menu_id},
  16. dataType: 'json',
  17. success:function (data) {
  18. //alert('ok');
  19. $('#ParentPage').html(data);
  20. console.log(data);
  21. },
  22. error:function(){
  23. alert('error...');
  24. }
  25.  
  26. });
  27.  
  28. }
  29. });
  30. });
  31. </script>



  1. <div class="form-group">
  2. <label for="GroupMenuPage">Gdzie wyświetlać</label>
  3. <select name="group_id" class="form-control" id="GrouspMenuPage" >
  4. <option value="0">No visible</option>
  5. <?php if($menu_list): ?>
  6. <?php foreach ($menu_list as $item) : ?>
  7. <option value="<?php echo $item['id']; ?>"><?php echo $item['name']; ?></option>
  8. <?php endforeach; ?>
  9. <?php endif; ?>
  10. </select>
  11. </div>
  12.  
  13. <div class="form-group">
  14. <label for="ParentPage">Parent</label>
  15. <select name="parent" class="form-control" id="ParentPage">
  16. <option value="0">No parent</option>
  17. </select>
  18. </div>


Ten post edytował codeszan 26.09.2017, 16:41:31
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.07.2025 - 10:02