title: How to send PHP data from database back to JQuery and insert to a Table
body: <p>As the title says, I'm trying to display data from a database based on user input and display it on a HTML table. In short I want the user input to filter the query request to the database, and then display the result in a table format. </p>
  
  <p>My issue is that I'm not exactly sure how to return the query result back to JQuery and then transform that data into a table in order to display it in HTML.</p>
  
  <p>This has been my approach so far but it doesn't work. I've already been successful in doing this with regular Javascript. But I want to try and use JQuery for this particular problem as it simplifies the code much more.</p>
  
  <p>JQuery:</p>
  
  <pre><code> var ourObj = {};
  
    ourObj.data = "Ranges";
    ourObj.arPoints = [{"SID":TABLID},{"T1": T1,"T2": T2},{"H1": H1,"H2": H2},{"P1": P1,"P2": P2},{"C1": C1,"C2": C2},
                       {"Y1": Y1,"Y2": Y2},{"M1": M1,"M2": M2},{"D1": D1,"D2": D2},{"m1": m1,"m2": m2}];
  
    $.ajax({
      type:"post",
      url:"GetQuery.php",
      data:{"data" : JSON.stringify(ourObj)},
      success: function(response){  
  
        $('SensorData').html(response);
  
      }
    });
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  if (isset($_POST["data"])) {
  
      // Decode our JSON into PHP objects we can use
      $data = json_decode($_POST["data"]);
  
      $id = $data-&gt;arPoints[0]-&gt;SID;
  
      $T1 = $data-&gt;arPoints[1]-&gt;T1;
      $T2 = $data-&gt;arPoints[1]-&gt;T2;
      $H1 = $data-&gt;arPoints[2]-&gt;H1;
      $H2 = $data-&gt;arPoints[2]-&gt;H2;
      $P1 = $data-&gt;arPoints[3]-&gt;P1;
      $P2 = $data-&gt;arPoints[3]-&gt;P2;
      $C1 = $data-&gt;arPoints[4]-&gt;C1;
      $C2 = $data-&gt;arPoints[4]-&gt;C2;
  
      $Y1 = $data-&gt;arPoints[5]-&gt;Y1;
      $Y2 = $data-&gt;arPoints[5]-&gt;Y2;
      $M1 = $data-&gt;arPoints[6]-&gt;M1;
      $M2 = $data-&gt;arPoints[6]-&gt;M2;
      $D1 = $data-&gt;arPoints[7]-&gt;D1;
      $D2 = $data-&gt;arPoints[7]-&gt;D2;
      $m1 = $data-&gt;arPoints[8]-&gt;m1;
      $m2 = $data-&gt;arPoints[8]-&gt;m2;
  
      // echo "Sensore ID: " . $id;
      // echo "Temperature: " . $T1 . " : " . $T2;
      // echo "Humidity: " . $H1 . " : " . $H2;
      // echo "Pressure: " . $P1 . " : " . $P2;
      // echo "CO: " . $C1 . " : " . $C2;
  
      $conn = mysqli_connect('localhost', 'root', '', 'sensors');
  
      if(empty($Y1) || empty($Y2) || empty($M1) || empty($M2) || empty($D1) || empty($D2) || empty($m1) || empty($m2)){
  
          $query = "SELECT * FROM sensors, sensorsdata WHERE sensors.SensorID = sensorsdata.SensorID 
          AND sensorsdata.SensorID = '$id' 
          AND sensorsdata.Temperature BETWEEN $T1 AND $T2
          AND sensorsdata.Humidity BETWEEN $H1 AND $H2
          AND sensorsdata.Air_Pressure BETWEEN $P1 AND $P2
          AND sensorsdata.Carbon_Monoxide BETWEEN $C1 AND $C2
          order by sensorsdata.Date Desc"; 
      }
      else{
          $time1= mktime(12,$m1,0,$M1,$D1,$Y1);
          $time1= date("Y-m-d h:i:s", $time1);
  
          $time2= mktime(12,$m2,0,$M2,$D2,$Y2);
          $time2= date("Y-m-d h:i:s", $time2);
  
          $query = "SELECT * FROM sensors, sensorsdata WHERE sensors.SensorID = sensorsdata.SensorID 
          AND sensorsdata.SensorID = '$id' 
          AND sensorsdata.Temperature BETWEEN $T1 AND $T2
          AND sensorsdata.Humidity BETWEEN $H1 AND $H2
          AND sensorsdata.Air_Pressure BETWEEN $P1 AND $P2
          AND sensorsdata.Carbon_Monoxide BETWEEN $C1 AND $C2
          AND sensorsdata.Date BETWEEN '$time1' AND '$time2'
          order by sensorsdata.Date Desc"; 
      }
  
      // Get Result
      $result = mysqli_query($conn, $query);
  
      // Fetch Data
      while ($row = mysql_fetch_array($result))
      {
         ?&gt;
            &lt;tr&gt;&lt;td&gt;&lt;?php echo $row['Temperature']?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;?php echo $row['Humidity']?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;?php echo $row['Air_Pressure']?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;?php echo $row['Carbon_Monoxide']?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;?php echo $row['Date']?&gt;&lt;/td&gt;&lt;/tr&gt;;
          &lt;?php   
      }
  
      mysqli_close($conn);
  }
  ?&gt;
  </code></pre>
  
  <p>HTML table</p>
  
  <pre><code>  &lt;table class="table"&gt;
                  &lt;thead&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Temperature&lt;/th&gt;
                          &lt;th&gt;Humidity&lt;/th&gt;
                          &lt;th&gt;Air Pressure&lt;/th&gt;
                          &lt;th&gt;Carbon Monoxide&lt;/th&gt;
                          &lt;th&gt;Date&lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody id="SensorData"&gt;
  
                  &lt;/tbody&gt;
              &lt;/table&gt;
  </code></pre>