button click to update table using ajax/post/php

button click to update table using ajax/post/php

mehtimehti Posts: 35Questions: 13Answers: 0

Hi guys,

I am trying to implement a function which calls php using ajax post function. I can extract the data and I can clear the table however for some reason I cannot get it to display the new output. here is my code

function getResults() {

            var transferTo;
            var transferFrom;
            var currency;
            var amount;

            transferTo = document.getElementById("transferTo").innerHTML.replace("<span class=\"caret\"></span>", "");
            transferFrom = document.getElementById("transferFrom").innerHTML.replace("<span class=\"caret\"></span>", "");
            currency = document.getElementById("currency").innerHTML.replace("<span class=\"caret\"></span>", "");
            amount = document.getElementById("amount").value;

            var postData = {
                "transferTo": transferTo,
                "transferFrom": transferFrom,
                "currency": currency,
                "amount": amount
            };

            if ($.fn.dataTable.isDataTable('#results')) {
                $('#results').DataTable.clear();
                $('#results').DataTable.ajax.reload();     
            }
            else {
                $('#results').DataTable({
                    "ajax":
                            {
                                url: "php/getMoneyTransferResults.php",
                                type: "POST",
                                dataType: "json",
                                data: postData
                            }
                });
            }
        }

Any help would be much appreciated.

Answers

  • mehtimehti Posts: 35Questions: 13Answers: 0

    I've managed to solve this. However, if anyone knows more elegant solution please do let me know.

                if ($.fn.dataTable.isDataTable('#results')) {
                    $('#results').DataTable().clear();
                    $('#results').DataTable().destroy();
                    $('#results').DataTable({
                        "ajax":
                                {
                                    url: "php/getMoneyTransferResults.php",
                                    type: "POST",
                                    dataType: "json",
                                    data: postData
                                }
                    });
                }
                else {
                    $('#results').DataTable({
                        "ajax":
                                {
                                    url: "php/getMoneyTransferResults.php",
                                    type: "POST",
                                    dataType: "json",
                                    data: postData
                                }
                    });
                }
    
This discussion has been closed.