1

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

5
  • You should know that you have a serious SQL injection vulnerability. SQL injection is one of the most dangerous web application vulnerabilities. Prepared statements are a good start on securing yourself: give them a google! Commented Oct 2, 2017 at 2:35
  • noted sir actually thats my target after achieving my problem here. Commented Oct 2, 2017 at 2:36
  • you can loop it inside the success and then append it to a table.. Commented Oct 2, 2017 at 2:58
  • the append table is ok for me but im having the problem in the loop part Commented Oct 2, 2017 at 3:00
  • You should set content type application/json at PHP end and T should be capital in ajax parameter dataType Commented Oct 2, 2017 at 3:35

2 Answers 2

1

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..

Sign up to request clarification or add additional context in comments.

11 Comments

Hi Sir thnx for the effort however but it gives me an error and sir can you give me a more detail sample because it is more than 1 column that i will display.
Sir Please bere with me I am trying to display 5 columns what should i do
here is the error Cannot use 'in' operator to search for 'length'
for example my select command has 4 columns and I will display that in table
Sir i pasted the error in my post. I just copy and paste the code youve given sir
|
0

You could try

$(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){
        $(data).each(function(i,res){
          $('table').append("<tr><td>"+res.vdate+"</td></tr>");
        });
      }
    });  
  });
});

1 Comment

oops..semicolon missing out there..fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.