$( document ).ready(function() { $("#btnsearch").click(function(e) { e.preventDefault(); $.ajax({ type: "POST", url: "/", data: $("form").serialize(), success: function(response) { if (response.s == "ok") { // console.log(response.r); var tbl_head = ""; var tbl_body = ""; if (response.r.length) { tbl_head = "#CodeCategoryNamePrice"; tbl_body += ""; $.each(response.r, function() { var tbl_row = ""; $.each(this, function(key, value) { if (key == "ee" || key == "aa") { tbl_row += ""+value+""; } else { tbl_row += ""+value+""; } }); tbl_body += ""+tbl_row+""; }); tbl_body += ""; } else { tbl_body = "No results"; } $("#results table").html(tbl_head+tbl_body); } else { $(".alert").html(response.e); $(".alert").fadeIn(); } }, error: function (xhr, ajaxOptions, thrownError) { $(".alert").html(thrownError); $(".alert").fadeIn(); } }); }); });