0

This is my script:

<script>
$(document).ready(function(){
    $("#ID_Blangko").on("change", function() {
        var blangko = $("#ID_Blangko").val();
        var baseUrl = '<?php echo base_url(); ?>program/administrasi/blangko_rusak/ajax_jumlah';
        $.ajax({
            url: baseUrl,
            data: {nama : blangko},
            dataType: "json",
            success: function(datas){
                $("#Jumlah_Blangko").val(datas);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#Jumlah_Blangko').val("some error.");
            }
        });
    });
});
</script>

and this is my controller code:

public function ajax_jumlah($nama)
{
    $this->db->select('Jumlah_Blangko');
    $this->db->where('Nama_Blangko', $nama);
    $result = $this->db->get('tb_blangko');
    $amount = $result->row()->Jumlah_Blangko;
    return json_encode($amount, JSON_NUMERIC_CHECK);
}

i've double checked the onchange function and controller function is working well and returning value. The problem is I cant pass this value to my ajax code and print it on input form. Here's my html:

<?php echo form_open($form_action, array('id'=>'myform', 'class'=>'myform', 'role'=>'form')) ?>
   <div class="row">
       <div class="col-md-6">
           <?php
                $atribut_blangko = 'class="form-control" id="ID_Blangko"';
                $selectedBlangko = $values->ID_Blangko;
                echo form_dropdown('ID_Blangko', $dropdown_Blangko, $selectedBlangko, $atribut_blangko);
            ?>
        </div>

        <div class="col-md-6">
            <?php echo form_input('Jumlah_Blangko', '', 'id="Jumlah_Blangko" class="form-control" placeholder="Jumlah" maxlength="50" readonly="true"') ?>
        </div>
   </div>
<?php echo form_close() ?>

update #2 and this solve my problem
this the controller function im accessing directly from browser URL that is http://localhost/amc/program/administrasi/blangko_rusak/ajax_jumlah/Malaysia
and i found out that
return json_encode($amount, JSON_NUMERIC_CHECK); this doesnt work and then i changed to:
echo json_encode($amount, JSON_NUMERIC_CHECK); this work.
I dont know how this is possible, anyone can explain?

6
  • not sure, what you mean by "I can't pass this value to my ajax code". Can you probably explain the issue - is an error popping up, is nothing happening, or do you need support understanding AJAX? In any case, in your first code snippet the 4th line (" var blangko = ID_Blangko.value;") is probably wrong as ID_Blangko is uniinitialized. try "var blangko = $(this).val();" instead. Commented Jun 5, 2016 at 17:52
  • @MatthiasHuttar i've update my question, and i updated my code from ID_Blangko.value into $("#ID_Blangko").val Commented Jun 5, 2016 at 18:04
  • I don't see in your code where the ajax_jumlah function being called. Apparently an argument is not being passed when you call the function. Commented Jun 5, 2016 at 18:34
  • @David the function is called using the URL see var baseUrl Commented Jun 5, 2016 at 18:39
  • add type: 'POST' do your ajax request and in your php you can retrieve the $nama variable by using $nama = $_POST['nama']; instead of passing it through the url Commented Jun 5, 2016 at 18:54

2 Answers 2

1

Please check the line no 3 in your code it should be "$("#ID_Blangko").val()" instead of "$("#ID_Blangko").val"

Explanation:

in the above code line you were expecting to get the value of the element having id "ID_Blangko" and you are using the jQuery, but the main thing here is that the "val" is a function and not a variable so you can not access the value by just saying "$("#ID_Blangko").val" because it looks for a property named as 'val'

Sign up to request clarification or add additional context in comments.

3 Comments

thanks now var blangko has value, but the ajax code still not getting value from the controller, and the error message still the same
Just to confirm that your server code has written properly, why don't you open the ajax url with proper params in the browser URL. see you getting the proper response.
sorry for my late response, the ajax URL returning correct value. the main problem now is how to send the value back to ajax?
1

I think

var blangko = ID_Blangko.value;

should be

var blangko = $('#ID_Blangko').val();

or

var blangko = $(this).val();

EDIT

Your problem with controller/action not seeing the variable could be because it expects params instead of query string vars like most frameworks do. Something like this could help you with that

        var baseUrl = '<?php echo base_url(); ?>program/administrasi/blangko_rusak/ajax_jumlah/' + blangko;

11 Comments

i changed that but still not getting result. i've found my error message, updated my question
Please try to change your base Url to this. var baseUrl = '<?php echo base_url(); ?>program/administrasi/blangko_rusak/ajax_jumlah/' + blangko;
still not working, and there's no error message either after adding blangko
is it possible that my framework doesnt support what im to achieve here? im using codeigniter
If there is no error message, your controller/action is receiving the variable (I presume). You can try debugging by using a echo $nama in your action and see. If you hardcode the $nama variable, are you getting back a json?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.