user1960257
user1960257

Reputation: 47

Jquery/AJAX function not working onChange Event

I am trying to populate a dropdown menu on behalf of the value of an other dropdown. the problem here is that the function i am trying to call in select tag's onChange event is not being compiled. and an error is appearing in console.enter code here. HTML:

<select name="street_id" id="street_id" onchange="get_data(this)" class="input-xlarge">

</select>

Jquery:

<script type="text/javascript">
    function get_data()
    {    
        $('#client_list').empty()    
        var dropDown = document.getElementById("street_id");
        var street_id = dropDown.options[dropDown.selectedIndex].value;
        $.ajax({
                type: "GET",
                url: "http://localhost:7777//index.php/admin/get_one_street_client/" + street_id,            
                data: { 'street_id': street_id },
                success: function(data){
                    // Parse the returned json data               
                    var opts = $.parseJSON(data);
                    // Use jQuery's each to iterate over the opts value
                    $.each(opts, function(i,d) {
                        console.log(d.client_name);
                        // You will need to alter the below to get the right values from your json object.  Guessing that d.id / d.modelName are columns in your carModels data
                        <?print "var value == '$balance_data[0]->client_id'";
                        //$selected = 'selected';
                        ?>
                        $('#client_list').append('<option value="' + d.client_id + ' ">' + d.client_name + '</option>');                    
                    });

                }

          });
    }
</script>

Error:

insert_balance:180

Uncaught ReferenceError: get_data is not definedonchange @ insert_balance:180

i have already added the jquery library address on top.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

Upvotes: 0

Views: 1286

Answers (1)

Emmanuel Delay
Emmanuel Delay

Reputation: 3679

Clearly, you should not put that php inside the success callback of the AJAX response. I can't imagine what good this could do. Just erase those lines.

I don't know what that php generates, but it looks like it generates a syntax error in function get_data().

This means the whole script (within the script tags) does not get compiled.

Also, it looks like that line of code isn't doing anything, even if it didn't cause problems

var value == 5 ; no idea why this code is present

Upvotes: 1

Related Questions