Jtable POST fails to send data (using PHP)

1k Views Asked by At

Having fun playing around with jtable,but got myself stuck in a bit of a rut.

I'm having a problem accessing data from a POST request. I'm trying to pass a variable from a field value in the table so I can make a custom SQL query.

My mainpage.php:

    $(document).ready(function () {
        //Prepare jTable
        $('#PeopleTableContainer').jtable({
            title: 'Trip data';
            ajaxSettings: {
            type: 'POST'
            },
            actions: {
                listAction: 'tableactions.php?action=list',
                updateAction: 'tableactions.php?action=update',
                deleteAction: 'tableactions.php?action=delete'
            },
            fields: {
                user_id: {
                    key: true,
                    edit: false,
                    list: false
                },
                name: {
                    title: 'Name',
                    edit: false,
                    list: false
                },
                trip_id: {
                    title: 'Trip ID',
                    list: false,
                    edit: false
                },
                trip_name: {
                    title: 'Trip Name',
                    width: '50%'
                },
                time: {
                    title: 'Start time',
                    width: '25%',
                    edit: false
                },
                date: {
                    title: 'Start date',
                    width: '25%',
                    edit: false
                }
            }
        });
        //Load person list from server
        $('#PeopleTableContainer').jtable('load');
    });   

I added the AJAX property to ensure it's set to post. Here's a portion of tableactions.php:

if($_GET["action"] == "list")
{
    //Get user ID
    $user_id = $_POST['user_id'];
    //SQL query
    $result = mysql_query("select * from userdata WHERE user_id=$user_id group by trip_id");
    //Add selected records into an array
    $rows = array();
    while($row = mysql_fetch_array($result))
    {
        $rows[] = $row;
    }
    //Return result to jTable
    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    $jTableResult['Records'] = $rows;
    print json_encode($jTableResult);
}

I've tried changing $user_id to a correct value manually and the table works and displays the correct information. Really racking my brains to try and solve this.

Have I missed something obvious? Why can't I get a value from $_POST?

Any help would be greatly appreciated!

1

There are 1 best solutions below

0
On BEST ANSWER

I fixed this by cleaning my code and adding this into the table properties:

ajaxSettings: {
            type: 'POST', 
            data: {'UserId': '<?php echo $userid;?>'},
            url: './tableactions.php'
            },

Works fine now.