Load value in input text after selecting value in select Codeigniter

Asked

Viewed 945 times

0

I want to load a value into an input text after selecting a value in a select. I am loading the normal select. I want to load input text using javascript. In my model I compare it with id. The data is in the same table.

Java Script:

<script>
    var base_url = '<? echo base_url() ?>';
    function busca_produtos(id_alug){
    $.post(base_url+"parcelas/get_parcelas", {
        id_alug : id_alug
    }, function(data){
        $('#parcelas').html(data);
    });
}</script>

My normal working select input:

        echo "<select name='aquiller' id='id' class='form-control input-sm' onchange='busca_produtos($(this).val())'>";

My Controller:

 public function get_parcelas(){
    $parcelas = $this->sindico->get_parcelas();
    $option = "<option value=''></option>";
    foreach($parcelas -> result() as $linha) {
        $option .= "<option value='$linha->id_alug'>$linha->id</option>";
    }
    echo $option;
}
  • @Magichat bro you can give a little pull in this problem?

  • Ramon do you want when a select value is selected that value to be placed in an input text is that? Or it would fetch another value based on the value of select?

  • It would fetch another value based on the value of select. You’ve seen my code. Can you tell me where I can get.

  • is giving this message: Message: Invalid argument supplied for foreach()

  • 1

    The variable $parcels -> result() is a valid array? can that be

  • @Ramonchaves I believe the comment above may be the point in question, foreach works with arrays, and result() seems to me a method.

Show 1 more comment

2 answers

0


I think your select should already be written in full on the page, the way it is in the code, it seems that only in the event change that the select is mounted.

Follow example to find the value based on the selected select

<select id="opcoes">
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
</select>

<input type="text" id="valor">

<script>
    $(document).ready(function() {
        $("#opcoes").change(function(event) {
            var id_alug = $(this).val();
            $.ajax({
                url: 'buscar_produto.php',
                type: 'POST',
                data: {id_alug : id_alug},
            })
            .done(function(valor) {
                $("#valor").val(valor);
            })
            .fail(function() {
                console.log("error");
            });
        });
    });
</script>
  • <script> var base_url = '<? php echo base_url()? >'; $(Document). ready(Function() { $("#options"). change(Function(Event) { var id_alug = $(this). val(); $. ajax({ url: 'base_url+"parcels/get_parcels"', type: 'POST', date: {id_lease : id_lease}, }) . done(Function(value) { $("#value"). val(value); }) . fail(Function() { console.log("error"); }); }); }); </script>

  • not running past the parameters of the controller and nothing

  • la na url get_parcelas is the name of my controller’s function

  • Message: Invalid argument supplied for foreach()

  • Is there any way we could adopt this model here? http://www.dicascodeigniter.com.br/howto create a dynamic selection/

  • But Invalid argument supplied for foreach() Is this a php error, is your query returning data? gives a var_dump on $parcels -> result() to see is being returned.

  • Fernando does not arrive at the model. He plays in javascript. You enter this link I sent you. The code of the guy ta normal. But in my dick. Because I want to show in input

  • From with console.log on return or send the javascript error message

Show 3 more comments

0

@fernandoandrade gives a look at how it turned out because in the comments gets messy.

<script>
    var base_url = '<?php echo base_url()?>';
    $(document).ready(function() {
        $("#opcoes").change(function(event) {
            var id_alug = $(this).val();
            $.ajax({
                    url: 'base_url+"parcelas/get_parcelas"',
                    type: 'POST',
                    data: {id_alug : id_alug},
                })
                .done(function(valor) {
                    $("#valor").val(valor);
                })
                .fail(function() {
                    console.log("error");
                });
        });
    });
</script>
  • Did the question code $.post work? I wonder if it is something related to how codeigniter works, I never used it, but as I saw an ajax request I answered the question. You as is your select on the page, already comes complete when correct click?

  • Yes select ta normal working. You’ll get the error msg that’s showing me. The error is in javascript

  • send the error message please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.