passing a variable to DataTables server side processing

passing a variable to DataTables server side processing

lschneidermanlschneiderman Posts: 17Questions: 5Answers: 0
edited June 2021 in DataTables 1.10

I have a dropdown box that I use to get a variable. Then I want to pass that variable to DataTables server side processing. The PHP page I use as the URL will take the variable to fill in a query. But I haven't been successful in passing this variable.

$('#medProcDataList').change(function() {
        
        //fetch variable
        var $selectedValue = $(this).val();
        var arrSelVal = $selectedValue.split(":");
        $code = arrSelVal[0]; //get hospital code

//Then I want to pass the variable to DataTables

$('#results').DataTable( {
            "processing": true,
            "serverSide": true,
            data: {code: $code },
            "ajax": "php/getDataForCode.php",
             "ajax": {
            "url": "scripts/server_processing.php",
            "data": $code
            columns: [
                    { title: "Hospital", data: "hospital_name" },
                    { title: "Description", data: "raw_description" },
                    { title: "Insurer", data: "full_payer_name" },
                    { title: "Insurer type", data: "play_type"},
                    { title: "You Pay", data: "price" }
            ]
        } );
}//end function

PHP:

$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
        printf("Connect failed: %s", mysqli_connect_error());
        exit;
}
mysqli_set_charset($db, 'utf8'); //important! or it won't echo the array

    if( $_POST) {
        $code = mysqli_real_escape_string($db, $_POST['code']);
        $data = array();
        $q = "SELECT hospital_name, raw_description, full_payer_name, plan_type, format([price], 'N0') FROM `hospital_transparency_data` where procedure_codes = '" . $code . "' order by hospital_name, full_payer_name";
        $result = $db->query($q);
        while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            //Add this row to the reply
            $data[] = $row;
        }
        
        
        $db->close();
        echo json_encode($data);
    
} //if POST

My query yields no results, so I know

What am I doing wrong please?

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    One problem is you have two ajax options configured. Only configure one ajax option. You also have data configured which shouldn't be used with ajax.

    The Datatables code snippet you posted is not syntactically correct. Maybe you posted only some of it? Looks like your columns option is inside the second ajax option.

    To pass data to the server use the ajax.data option as a function. The docs have a couple examples plus here is a running example.

    Kevin

Sign In or Register to comment.