How to populate datas into a select box using jQuery ajax json, php



If you are wondering how to populate select box using jquery ajax then you may find the below post very simple and useful.

Retrieve values and populate the select box using jquery, ajax and PHP.

 
 
$.ajax({
    type: "POST",
    url: 'http://www.appsntech.com/demo/rubro/list_ajax',
    data: {'categoryID': $("#category").val(),'isAjax':true},
    dataType:'json',
    success: function(data) {

       var select = $("#select"), options = '';
       select.empty();      

       for(var i=0;i<data.length; i++)
       {
        options += "<option value='"+data[i].id+"'>"+ data[i].name +"</option>";              
       }

       select.append(options);
    }
});

And now in your server side page you may write below to fetch data from database.

    

if (isset($_POST['categoryID'])) {
    $id = trim($_POST['categoryID']);
    $result = array();
    $id = mysql_real_escape_string($id);
    $res = mysql_query("SELECT * FROM subcategories WHERE category_id = $id");
    while ($row = mysql_fetch_array($res)) {

    $result[] = array(
      'id' => $row['subcatid'],
      'name' => $row['description']
    );

    }
    echo json_encode($result);
}



   

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions