0

i`m trying to fill a combo box basis on another combo box in php using ajax.i created two files category_dropdown.php and subcategory_dropdown.php.

my category_dropdown.php file as follow.

<?php
include 'connect.php';
$q = mysql_query("select * from category") or die(mysql_error());
if (mysql_num_rows($q)) {
$data = array();
while ($row = mysql_fetch_array($q)) {
$data[] = array(
'id' => $row['category_id'],
'name' => $row['category_name']
);
}
header('Content-type : application/json');
echo json_encode($data);
}

my subcategory_dropdown.php file as follow.

<?php
include 'connect.php';
if(isset($_GET["catname"])){
$c=$_GET["catname"];
$q=  mysql_query("select * from subcategory where cat_id='$c'") or         die(mysql_error());
if(mysql_num_rows($q)){
$data=array();
while($row=  mysql_fetch_array($q)){
$data[]=array(
'id'=>$row['sub_id'],
'name'=>$row['sub_name'],
);
}
header('Content-type : application/json');
echo json_encode($data);
}else{
echo 'error';
}
}

then i used follw javascript to fillcomboxes

<script type="text/javascript">
    $(document).ready(function() {
        Category();
        $("#cat").change(function() {
            var textbox3 = document.getElementById('cat');
            val = textbox3.value;
            Sub(val);
        });
    });


    function Category() {
        $('#cat').empty();
        $('#cat').append("<option>Loading....</option>");
        $('#subcat').append("<option>--Select SubCategory--</option>");
        $.ajax({
            type: "POST",
            url: "category_dropdown.php",
            contentTYpe: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                $('#cat').empty();
                $('#cat').append("<option value='0'>--Select Category--</option>");
                $.each(data, function(i, item) {
                    $('#cat').append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
                });
            },
            complete: function() {}
        });
    }

    function Sub(catname) {
        $('#subcat').empty();
        $('#subcat').append("<option>Loading....</option>");
        $.ajax({
            type: "POST",
            url: "subcategory_dropdown.php?catname=" + catname,
            contentTYpe: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                $('#subcat').empty();
                $('#subcat').append("<option value='0'>--Select SubCategory--</option>");
                $.each(data, function(i, item) {
                    $('#subcat').append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
                });
            },
            complete: function() {}
        });
    }
</script>

But when i run this function my first drop down box(category_dropdown.php) does not fill.It says only "Loading........".

why is that.where i have problem. please help.

Sushil
  • 2,837
  • 4
  • 21
  • 29
lahiruk93
  • 69
  • 1
  • 1
  • 8
  • 1
    Do the PHP code returns any error? You should use a JS console to check what is returned and be able to debug this. –  Jun 08 '15 at 16:30
  • hi, when i try to run category_dropdown.php it returns nothing.output is null.i think that`s the problem.do you have any idea why this happen? – lahiruk93 Jun 08 '15 at 16:52
  • Have a look at it: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php –  Jun 08 '15 at 16:56
  • now i get an error "Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future " i read some article about this in stackoverflow.i`m confusing because i use this same codes for a another project in my computer.it is working well.but this is not.how is it possible? – lahiruk93 Jun 08 '15 at 17:40
  • Here's an explanation about deprecated functions: http://stackoverflow.com/questions/6822446/what-does-php-do-with-deprecated-functions . It doesn't prevent your code from working but it might be an issue if you upgrade your PHP version. It's better to start using another function/extension. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php –  Jun 09 '15 at 08:07

1 Answers1

0

My recommendation would be to run your query first and test your SQL. If you know your SQL is working then I would test your PHP and make sure the logic is working by setting a some static test variables. If that works, then you know its more than likely your AJAX calls. Also try running the Console window in your browser as caCtus recommended. JS Console can be a lifesaver. It may seem a bit tedious at first, but ruling out each technology layer is worth it as it provides better isolation and identification for where the problem lays.

Alex
  • 443
  • 3
  • 18
  • now i get an error "Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future " i read some article about this in stackoverflow.i`m confusing because i use this same codes for a another project in my computer.it is working well.but this is not.how is it possible? – lahiruk93 Jun 08 '15 at 17:47
  • Are you using a different version in your prior project? You may also be utilizing a syntax or feature of the latest PHP release which is conflicting with mysql_connect();. Without knowing how both projects are configured, its hard to provide an exact answer. – Alex Jun 08 '15 at 17:56
  • no i`m using same version in both projects . actually these codes are copied from previous project. i don`t know whats going on.i tried to fill combo box manually only using php in my index.php. then it is working.but it is not working on category_dropdown.php file. – lahiruk93 Jun 08 '15 at 18:07