New to dataTables. how to I pass a value to server-side.php

New to dataTables. how to I pass a value to server-side.php

fkamwazafkamwaza Posts: 2Questions: 1Answers: 0

I need to change the "where-clause" in the sql statement that is used to fetch data from a db and display it in dataTables by changing the value of myInput and passing it on to the server-side. I have this code:

<input id=myInput value="london">

<script>
$(document).ready(function(){
$('#example').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "server_processing.php",
        "data": function ( d ) {
            d.myInput = $('#myInput').val();
        }
    }
    });
} );
</script>

Server-side has this code:

<?php
//...HERE I have defined these: $sql_details, $table, $primaryKey, $columns 
$myInput =$_POST['myInput'];
$where = " `office` = '$myInput' ";
require( 'ssp.class.php' );
echo    json_encode(SSP::simple($_GET,$sql_details,$table,$primaryKey,$columns,$where)); 

How to I pass a value to server-side.php, this line is not being accepted: $myInput =$_POST['myInput'];

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    The request is being made as a GET request, but you are trying to read a POST variable. They need to match. Change one of them to match the other.

    Allan

  • fkamwazafkamwaza Posts: 2Questions: 1Answers: 0

    Thank for the pointer.

    I have achieved what I needed with the following code. However I feel there might be a shortcut to this.

    $(document).ready(function() {
        $("#upload").click(function(){
            $.post("testdd.php",{
                'nameid':    $("#nameid").val(),
                'surname':   $("#surname").val(),
                'firstname': $("#firstname").val(),
            },
            function(data, status){
                drawTable(data); 
            });
        });
    
        function drawTable(dataSet){
            $('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display main" id="example"></table>' );
            $('#example').dataTable( {
                "destroy": true,
                "processing": true,
                "data": dataSet,
                "columns": [
                    { "title": "NameID" },
                    { "title": "Surname" },
                    { "title": "Firstname" }
                ]
            });
        }
    });
    
This discussion has been closed.