0

I am inserting data with jquery Ajax using form submit, And fetching data to dataTable after form submit, following code giving DataTables warning: table id=mainTable - Requested unknown parameter '1' for row 0, column 1. . . . .

var mainTable = $('.mainTable').DataTable();
$('#Form').submit(function(e) {
  e.preventDefault();
  var form = $(this).serialize();
    $.ajax({
        url: "result.php",
        type: "post",
        data: form 
  }).done(function (data) {
        mainTable .clear().draw();
        mainTable .rows.add(data).draw();
        }).fail(function (jqXHR, textStatus, errorThrown) { 
        });
});

HTML

<div class="content">
    <!-- form start -->
    <form method="post" id="Form">     

            <input type="text" name="id">
            <input type="text" name="name">
            <input type="text" name="class">

        <button type="submit">submit</button>
    </form> 


<table id="mainTable" class="mainTable table table-striped">
  <thead>
    <tr>
      <th>Roll No</th>
      <th>Name</th>
      <th>class</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
</div>

PHP

$id = $_POST['group_id'];
$rollno = $_POST['roll_no'];
$name = $_POST['name'];
$class = $_POST['class'];

$insert = "insert into students (roll_no,group_id,name,class) values(:roll_no,:id,:name,:class)";
$insert = $db->prepare($insert );
$insert ->bindParam(':roll_no',$rollno);
$insert ->bindParam(':id',$id );
$insert ->bindParam(':name',$name);
$insert ->bindParam(':class',$class);
$insert ->execute();

$fetch = "SELECT roll_no,name,class FROM students where group-id=:id";
$fetch = $db->prepare($fetch );
$fetch ->bindParam(':id',$id);
$fetch ->execute();

$output = array('data' => array());
while($row = $fetch ->fetch(PDO:: FETCH_OBJ)) {

    $id = $row->roll_no;
    $name = $row->name;
    $class = $row->class;
$output['data'][] = array( $id,$name,class);    
} // /while 

echo json_encode($output);
lipon
  • 11
  • 1
  • 14
  • It's difficult to replicate this without JSON response example. But my wild guess is the error is because you put the array of records inside `data` attribute. Have you tried to use `mainTable.rows.add(data.data).draw();` instead? If it's still not working, you may want to post JSON response example – Yohanes Gultom May 25 '19 at 13:14
  • @YohanesGultom its giving error `Uncaught TypeError: Cannot read property 'length' of undefined at s.` with `mainTable.rows.add(data.data).draw();`. – lipon May 25 '19 at 14:08
  • Since you haven't provide any additional data, I'll make another wild guess: the json response is not automatically parsed due to lack of header. In that case you want to try `data = JSON.parse(data);mainTable.rows.add(data.data).draw(); ` – Yohanes Gultom May 25 '19 at 14:32
  • @YohanesGultom thats it, its working now, thank you. – lipon May 25 '19 at 17:14

1 Answers1

0

Moved from comment

The root cause is simply because jQuery ajax() doesn't parse the JSON response from result.php. The easiest solution is to parse it inside done():

data = JSON.parse(data);
mainTable.rows.add(data.data).draw();

Another solution is to properly return JSON header in result.php. Line data = JSON.parse(data) in done() should also be omitted:

header('Content-Type: application/json');
echo json_encode($output);
Yohanes Gultom
  • 3,782
  • 2
  • 25
  • 38