paul pagente paul pagente - 3 years ago 169
Ajax Question

Loop to every each row of php array

Hello I have 2 sets of code here and here it is

Ajax


/* Loop and Get Data from database to create a table */
$(document).ready(function () {
$('#btngenerate').click(function(e){
var d1 = $('#startdate').val();
var d2 = $('#enddate').val();
$.ajax({
url: 'queries/qryTITO.php',
type: "POST",
datatype: 'json',
data: ({startdate: d1,enddate: d2}),
success: function(data){
console.log(data);
}
});
});
});


and here is the
PHP


<?php
require 'conn.php';
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$sql = "SELECT vdate FROM tablename WHERE date(vdate) between date('" . $startdate . "') and date('" . $enddate . "')";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_row($result);
$jsonData = array();
while ($array = mysqli_fetch_row($result)) {
$jsonData[] = $array;
}
echo json_encode($jsonData);
mysqli_close($con);
?>


here is my question. I am trying to return the value back to the jquery so I can create a HTML Table. actually it is working and this is the output

enter image description here

My question here is how can I make an HTML table for that data?

Here is the error i get

enter image description here

Answer Source

Try this

$(document).ready(function () { 
   $('#btngenerate').click(function(e){
       var d1 = $('#startdate').val();
       var d2 = $('#enddate').val();
         $.ajax({
            url: 'queries/qryTITO.php',
            type: "POST",
            datatype: 'json',
            data: ({startdate: d1,enddate: d2}),
            success: function(data){
                    $.each(data,function(){
                        $('tr').append("<td>"+this+"</td>")
                    });
            }
        });  
   });
});

here is a Sample Fiddle..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download