Fetch data from DB and display in a dynamically generated select box using jquery and php
I have the code that dynamically generates textboxes and select boxes upon a button click. I want to fetch the data from DB and display in the dynamically generated select box.
Please see the fiddle for HTML and complete jquery code.
I have tried the following part of code to fetch the data from DB and Put into dynamically generated select box(Tax Type) but its not working for me.
- //To Display the tax types from DB
- $(function(){
- var items="";
- $.getJSON("get_tax_type.php", function(data) {
- alert(data);
- $.each(data, function(index, item) {
- $('#tax_type' + counter + '' + parseInt(index) + parseInt(1)).empty();
- $('#tax_type' + counter + '' + parseInt(index) + parseInt(1)).append("<option value='" + item.TaxID+ "'>" + item.TaxName+ "</option>");
- });
- }, 'json');
- });
PHP file(
(get_tax_type.php)
I feel the way I am doing is wrong.Can anybody suggest where am I doing wrong or the proper way of implementing it. I am newbie in jquery. Any help is appreciated.Thanks in advance.
<?php include('includes/db.php');
$q = "select TaxID, TaxName from tax";
$sql = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($sql, true))
{
$data[] = $row;
};
echo json_encode($data);
?>