Codeigniter3 - Get data from a controller by sending an id with ajax

0

I have been reviewing the site and I have not been able to find an answer to my question. I'm trying from a select with a list of products that I have in a view that when I select one I get updated the existence data of that product that I get from a controller function. I know how to get them with php, what I need is to achieve it on the same page, without recharging it, but I'm not very skilled in javascript and I'm a bit lost. Greetings and thanks in advance.

    
asked by Delvis Díaz 22.03.2018 в 19:54
source

2 answers

1

In your view you can add a script and by javascript you get the value either with a document.getElementById('tu_selector') after this you send your id by ajax to the controller, and the answer in the .done will make you adapt it without having done a reload of the page or submit or something like that.

  

Summon the JS element

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



    <script>
$(document).on('ready',function(){   
$('#mi_selector').on('change', function() {
              // Para determinar el cambio de valor
    var id = $('#mi_selector).val();
    //alert(id);
              $.ajax({                        
               type: "POST",                 
               url: "mi_controlador", //recordar sin el .php                    
               data: id, 
               success: function(data)             
               {
                 $('#resp').html(data);               
               }
           });
            });
});
</script>

then in your controller you will receive the variable and do with it what you want to return an answer.

$id = $_POST['data'];
    
answered by 22.03.2018 / 20:17
source
0

What I have been able to do so far is:

<script type="text/javascript">
    document.getElementById('product_id'){
        if(id) {
            $.ajax({
                  url:"<?=site_url("storemov/getID/$row->product_id")?>",
                  type:"POST",
                  dataType: 'json',
                  success:function(respuesta){
                      window.location.href = "<?=site_url("storemov/create/$row->product_id")?>";
                  }
            });
        }
    }
</script>

Where ('product_id') is my select, storemov/getID/$row->product_id is the call to the method that filters the query, is the page that should reload

    
answered by 22.03.2018 в 20:34