Claudio Martinez
Claudio Martinez

Reputation: 295

How to pass and get data from Ajax to PHP using Data-tables

I'm trying to use Data-tables but I need to pass a value from Ajax to PHP file.

the Ajax part is like this:

<script>
    $(document).ready(function() {

        var oTable =
            $('#user-list').DataTable({
                "serverSide": true,

                "ajax": {
                    "url": "assets/server_processing_reminders.php",
                    "data": {
                        "CurrentFlag": 1
                    }
                },

                "columnDefs": [{
                    "width": "6%",
                    "targets": 0
                }],


                "order": [
                    [1, "asc"]
                ]

            });


    });
</script>

on the server side Im trying to get the variable "CurrentFlag" using:

<?php

if (isset($_GET["CurrentFlag"])){
    $cf = $_GET["CurrentFlag"];
}

echo $cf;

but the php file is not printing out the value send. Thanks for any help

Upvotes: 1

Views: 1184

Answers (3)

Sanal S
Sanal S

Reputation: 1152

You need to provide request type as GET as shown here.

"ajax" : { 
    "url": "assets/server_processing_reminders.php", 
    type: "GET", 
    "data": { 
            "CurrentFlag": 1 
    } 
}

Upvotes: 1

Sarath V R
Sarath V R

Reputation: 23

Use Jquery for better code and results.

Learn Ajax using this link :

https://www.w3schools.com/js/js_ajax_intro.asp

Upvotes: 0

Ashish Detroja
Ashish Detroja

Reputation: 1082

Please use $_REQUEST instead of $_GET like this :

 if(isset($_REQUEST["CurrentFlag"]))
  {
    $cf = $_REQUEST["CurrentFlag"];

   }

   echo $cf;

OR

If you want print data using $_GET method please add type:GET under ajax call

Upvotes: 1

Related Questions