Ajax inside Datatables Custom Button Action

385
August 19, 2017, at 06:03 AM

I want to store datatables data inside an array or json format and use it to pass to php for FPDF use. In my datatable I created a custom button named "PDF", when the button is click I want to store the data and pass it into php via post. I have this code, but it wont work if I put ajax inside the action function of the custom button I made. It doesnt go to the url I declared. Here's the code:

EDITED

$(document).ready(function() {
        var table = $('#stud_list').DataTable({
            dom: 'Blfrtip',
            columnDefs: [{
                targets: 1,
                className: 'noVis'
            }],
            buttons: [{
                    extend: 'excelHtml5',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'csvHtml5',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    text: 'PDF',
                    exportOptions: {
                        columns: ':visible'
                    },
                    action: function(e, dt, node, config) {
                       $.ajax({
                            url: 'pdfViewStudent.php',
                            type: 'post',
                            data: table.row().data().toArray(),
                            dataType: 'json',
                            success: function(returnedData) {
                                console.log(returnedData);
                            }
                        });
                    }
                },
                'colvis'
            ],
            columnDefs: [{
                targets: -1,
                visible: false
            }],

I edited the code, the error was gone but nothing happens also.

ADDED HTML CODE:

<table id="stud_list" class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Last Name</th>
                                        <th>First Name</th>
                                        <th>Initials</th>
                                        <th>Grade</th>
                                        <th>Section</th>
                                        <th>Gender</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                        $result = mysqli_query($connect, "SELECT * FROM tbl_student") or die(mysqli_error());
                                        while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
                                        ?>
                                    <tr>
                                        <td>
                                            <?php echo $row['student_id']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['last_name']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['first_name']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['Initials']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['gradeLevel_id']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['section_id']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['gender']; ?>
                                        </td>
                                        <td>
                                            <a class="btn btn-success btn-xs" href="studentProfile.php?studId=<?php echo $row['student_id']; ?>"><i class="fa fa-eye fa-1x"></i></a>
                                            <button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#editStudent" data-studaydi="<?php echo $row['student_id']; ?>" data-cardaydi="<?php echo $row['card_id']; ?>" data-seksyon="<?php echo $row['section_id']; ?>" data-gardyan="<?php echo $row['Parents_No']; ?>" data-location="<?php echo $row['address']; ?>" data-ln="<?php echo $row['last_name']; ?>" data-fn="<?php echo $row['first_name']; ?>" data-mn="<?php echo $row['Initials']; ?>" data-gn="<?php echo $row['gender']; ?>" data-cy="<?php echo $row['cyear']; ?>" data-gr="<?php echo $row['gradeLevel_id']; ?>"><i class="fa fa-edit fa-1x"></i></button>
                                            <a onclick="javascript: return confirm('DO YOU REALLY WANT TO DELETE THIS STUDENT?');" href="removeStudent.php?id=<?php echo $row['student_id']; ?>" class="btn btn-danger btn-xs"><i class="fa fa-remove fa-1x"></i></a>
                                        </td>
                                        <?php } ?>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                    </tr>
                                </tfoot>
                            </table>
Rent Charter Buses Company
READ ALSO
Missing &#39;extra&#39; folder in /conf/apache

Missing 'extra' folder in /conf/apache

I went through MAMP's install with no issues (now multiple times) and uncommented the Include conf\extra\httpd-vhostsconf line in the httpd

167
keep on same bootstrap tab on form submit( page refresh )

keep on same bootstrap tab on form submit( page refresh )

I have bootstrap tabs with different formsOn TAB 2 I have form of 4 inputs and for sure button

249
I can upload images and files on my website on desktop fine, but on mobile it doesnt work

I can upload images and files on my website on desktop fine, but on mobile it doesnt work

I have a separate gallery and file sectionBoth which work fine on desktop but not mobile

178