Ajax failed to pass data to php server side processing

Ajax failed to pass data to php server side processing

jiejiangjiejiang Posts: 12Questions: 2Answers: 0
edited May 2022 in Free community support

Link to test case:
Debugger code (debug.datatables.net): https://debug.datatables.net/ilazub
Error messages shown:
Description of problem:

I want to make a ajax call to process the table on server side. If I don't pass any data by ajax to the server side, the code works just fine and the table can be updated from the server side data. But when I want to pass some parameters to server side for filter, it doesn't work. My php gets empty array() for both $_GET and $_POST. I have tried different set up with ajax (e.g contentType, async etc)but none of them worked. I don't know whehter it is because of the ajax or the php configuration.

The datatable:

  $(document).ready(function() {
       $("#example").DataTable({
            "destroy": true,
            "paging": true,
            "serverSide": true,
            "hover":true,
            "utoWidth":true,
            "processing":true,
             "searching": false,
           "lengthMenu": [10,15,20,25],
           "order": [[9, 'desc']],
            "info" : true,
             "sScrollX":true,

                                columns: [

                                {data: "dRid"},
                                {data: "chromosome"},
                                {data: "position"},
                                {data: "strand"},
                                {data: "cell_line"},
                                {data: "modification"},
                                {data: "geneId"},
                                {data: "species"},
                                {data: "rbp"},
                                {data: "mirna"},
                                {data: "splicingsite"},
                                {data: "snp"}

                            ],
                               ajax:({
                                type:"GET", // POST also tried
                                url:"./php/search_test.php",
                                datType: 'JSON',       
                                data: {"species": species}

                                    })

 } );
} );


    <?php
    ini_set ('memory_limit',  '2048M');


    $sql_details = array(
        'host' => 'localhost',
        'user' => 'root',
        'pass' => '',
        'db'   => 'directrmdb'
    );

    // DB table to use
    $table = 'homo_sapiens';

//if I try this here: $species=$_GET(or POST)["species"]; echo $species;It
//returns: Warning: Undefined array key "species". Without it, the code worked fine and the table can be returned to html page.

    // Table's primary key
    $primaryKey = 'dRid';

    // column
    $columns = array(
        array( 'db' => 'dRid', 'dt' => 'dRid' ),
        array( 'db' => 'chr', 'dt' => 'chromosome' ),
        array( 'db' => 'pos',  'dt' => 'position' ),
        array( 'db' => 'strand', 'dt' => 'strand' ),
        array( 'db' => 'cell_line', 'dt' => 'cell_line' ),
        array( 'db' => 'modification', 'dt' => 'modification' ),
        array( 'db' => 'geneId', 'dt' => 'geneId' ),
        array( 'db' => 'species', 'dt' => 'species' ),
        array( 'db' => 'RBP_number', 'dt' => 'rbp' ),
        array( 'db' => 'miRNA_target_number', 'dt' => 'mirna' ),
        array( 'db' => 'SplicingSite_number', 'dt' => 'splicingsite' ),
        array( 'db' => 'SNP_number', 'dt' => 'snp' ),
        array( 'db' => 'species', 'dt' => 'species' )

    );

    // Include SQL query processing class
    require( 'ssp.class.php' );




    echo json_encode(
      SSP::complex( $_GET, $sql_details, $table, $primaryKey,$columns)
    );


    ?>

Could someone help me with it? Thank you.

Answers

  • tokrishtokrish Posts: 12Questions: 1Answers: 0

    See this Add Parameters

  • jiejiangjiejiang Posts: 12Questions: 2Answers: 0

    I want to pass static paramters rathe than a function. I suppose I shall use format like data:{"species" : speicies} ?

  • allanallan Posts: 61,431Questions: 1Answers: 10,048 Site admin

    Assuming your speicies variable doesn't change value, then yes that will work just fine (documentation for it is here: ajax.data). If it can change value and you need to update the data based on that, then yes, you must use a function.

    Allan

  • jiejiangjiejiang Posts: 12Questions: 2Answers: 0

    I tried both function and data: {"species": species} but neither works. The problem is the ajax call seems working because I do have a table displayed on the page with server side data. However, no matter which method I use, I cannot pass any data to the php file. $_POST and $_GET are always empty.

  • allanallan Posts: 61,431Questions: 1Answers: 10,048 Site admin

    Where are you setting the Javascript variable species? I don't see it anywhere in the above code. I'm wondering if it is actually undefined at that point?

    If you can give me a link to your page showing the issue, I could say for sure.

    Thanks,
    Allan

  • jiejiangjiejiang Posts: 12Questions: 2Answers: 0
    edited May 2022

    Hi Allan,

    The part with 'species':

    <script language='javascript' type="text/javascript">
    //Extract keywords from url
      function getParams(key) {
              var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)","i");
              var r = window.location.search.substr(1).match(reg);
              if (r != null) {
                  return unescape(r[2]);
              }
              return null;
          };
    
        function trimStr(str){return str.replace(/(\+)/g, " ");}
            var searchtype=trimStr(getParams("Type"));
            var species=trimStr(getParams("Species"));
            var search=trimStr(getParams("keyword"));
      </script>
    

    I console.log(species); before the datatable function and it shows 'homo_sapiens' in the console. So I think it is defiend without problem. Thx.

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

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    To progress this, it would really help to see your page as Allan asked. Could you PM the details to him (or me), please, and we can take a look,

    Colin

  • jiejiangjiejiang Posts: 12Questions: 2Answers: 0

    Hi Allan and Colin,

    I sent a message to Allan with the code. Could you please help me to check it? Thanks!

  • allanallan Posts: 61,431Questions: 1Answers: 10,048 Site admin

    Thanks for the plnkr link. The request there is including your parameter - from the Firefox network console:

    You should be able to access that in your PHP script as $_GET['species'].

    Allan

  • jiejiangjiejiang Posts: 12Questions: 2Answers: 0

    Hi Allan,

    I managed to get the data from mySQL but my php still have the error message 'Undefined array key "species"'. I guess I just leave it as it is as long as the results can return.

    Thank you very much for your kindly help!

  • allanallan Posts: 61,431Questions: 1Answers: 10,048 Site admin

    Can you show me the full PHP script please?

    Allan

Sign In or Register to comment.