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.

    type: "POST",
    url: '',
    data: {'categoryID': $("#category").val(),'isAjax':true},
    success: function(data) {

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

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


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);



  1. Thanks a loto for this script! It was what I' was looking for!

    1. Most welcome Maria..... Keep visiting for new posts here....


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 All rights reserved. Powered By- Sanjoy Dey Productions