ajax response to datatables sqlsrv with ajax & php pagination

ajax response to datatables sqlsrv with ajax & php pagination

nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0
edited March 2016 in Free community support

I am selecting a value(state) from dropdown Page (send.php), after i am processing filter state data in (response.php), this data need to display page (send.php), i am try to use but getting raw json data, it's not showing in datatbles.How to show data from json response into datatable.

Answers

  • allanallan Posts: 63,276Questions: 1Answers: 10,424 Site admin

    Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.

    Information on how to create a test page], if you can't provide a link to your own page can be found here.

    Thanks,
    Allan

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0
    edited March 2016

    this code is from ( send.php )

    <

    script type="text/javascript">
    function showUser()
    {
    var state = document.getElementById('state').value;
    if (state=="")
    {
    document.getElementById("example1").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //function test();
    var a = xmlhttp.responseText;
    alert(a);

      document.getElementById("example1").innerHTML=xmlhttp.responseText;
    document.getElementById("loading").innerHTML = ''; 
    }
    

    }
    document.getElementById("loading").innerHTML = '<img src="35.gif" />';
    xmlhttp.open("GET","getuser.php?state="+state+,true);
    xmlhttp.send();
    }

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0

    My json code from response.php

    {"draw":1,"iTotalRecords":1,"iTotalDisplayRecords":1,"aaData":[{"ID":1731166,"INDUSTRY":"3545 Cutting Tools Machine Tool Accessory & Precision Measuring Device Mfg","COMPANYNAME":"Curran Manufacturing Corp","TITLE":"President","NAICS":null,"SICCODES":"3545"}]}

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0

    Running environment is Windows server, XAMPP, PHP, MSSQL (sql server 2012)

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0
  • allanallan Posts: 63,276Questions: 1Answers: 10,424 Site admin

    Thanks for the link to the JSFiddle - however it gives a number of syntax errors. I'm presuming you don't have those syntax errors locally. Can you link to a running test case so the issue can be debugged please.

    Allan

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0
  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0

    I can get results from the get.php but problem is showing raw json.i am get ajax response text, so how to convert ajax json response to datatables.

    Above mentioned link is simplified for php and mysql ( but my actual work is in Intranet - php,mssql-sqlsrv windows OS)

  • allanallan Posts: 63,276Questions: 1Answers: 10,424 Site admin

    Use $.parseJSON if you want to convert a JSON string to actual JSON. Moreover, I would suggest you use $.ajax or $.getJSON to get the JSON data rather than writing your own Ajax handler.

    Once you have the data, use rows.add() to add the data. Also, remove the server-side processing option unless you are actually going to have DataTables make the Ajax requests for you.

    Allan

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0

    Thank you very much for supporting. Finally it's working fine

  • nnkrishnanannkrishnana Posts: 9Questions: 1Answers: 0
    edited March 2016
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
          var data = xmlhttp.responseText;
          //alert(data);
          var result = jQuery.parseJSON(data);
          //alert(result);
          
          var test = $('#example1').dataTable //example1 is table id
          ({
          "aaData": result,
          "columns": [
                  { "data": "ID" },
                  { "data": "COMPANY" },
                  { "data": "SALARY" }
               ],
          //"bprocessing": true,
              //"iDisplayLength": 50000,
          "bserverSide": true,
          "deferRender": true,
          "bPaginate": true,          
          //"bSort": true,
          //"bSortClasses": false,
          "bProcessing": true,
          //"bDeferRender": true,
          "bDestroy":true,
              "bJQueryUI": true,
              "bSort": false,
              "bSortClasses": false,
              "deferLoading": 57,
              "sPaginationType": "full_numbers",
              "aLengthMenu": [[10, 25, 50, 100, 250, 500, 1000, 5000, -1], [10, 25, 50, 100, 250, 500, 1000, 5000, "All"]]
        
            });    
        document.getElementById("txtHint").innerHTML=test;      
        document.getElementById("loading").innerHTML = ''; // Hide the image after the response from the server
       //document.getElementById("1").innerHTML=xmlhttp.responseText;
        }
      }
    
This discussion has been closed.