Can't retrieve extra parameter passed to Ajax in client-side mode

Can't retrieve extra parameter passed to Ajax in client-side mode

dudaneskdudanesk Posts: 21Questions: 5Answers: 0
edited October 2017 in Free community support

Hello everyone,
On client-side mode, I pass an extra parameter to Ajax php file, but on that PHP script I can't seem to retrieve that parameter.

JAVASCRIPT:

        var iniContinent="processing";
        var continentTable = $('#filterContinent').DataTable( {
            select: {
                style: 'multi'
            },
            "ajax": 'c_continent.php',
            'columnDefs': [
                {
                    'targets': 0,
                    'checkboxes': {'selectRow': true},
                    'createdCell':  function (td, cellData, rowData, row, col){
                        this.api().cell(td).checkboxes.select();
                    },
                },
                {'targets': 1, 'orderable': false},
            ],
            "fnInitComplete": function(oSettings, json) {
                iniContinent="done";
                countryTable.ajax.reload();
            }
        } );
        
        var countryTable = $('#filterCountry').DataTable( {
            'order': [[1, 'asc']],
            'columnDefs': [
                {
                    'targets': 0,
                    'checkboxes': {'selectRow': true},
                    'createdCell':  function (td, cellData, rowData, row, col){
                        this.api().cell(td).checkboxes.select();
                    },
                },
                {'targets': 1, 'orderable': false},
            ],
            select: {
                style: 'multi'
            },
            "ajax": {
                "url": "c_country.php",
                "type": "POST",
                "data": function ( d ) {
                    var retrieveIDs="";
                    var myIDs = continentTable.rows('.selected').data().toArray();
                    for ( var i=0 ; i<myIDs.length ; i++ ){
                        retrieveIDs += myIDs[i][0]+",";
                    }
                    d.continentIDs = retrieveIDs;
                }
            },
            "fnInitComplete": function(oSettings, json) {
                iniCountry="done";
                stateTable.ajax.reload();
            }
        } );

Here, when I initialize or reload countryTable, I try to pass in parameter the IDs of all rows selected in continentTable. I feel like I have no issues so far. But...

PHP SCRIPT "c_country.php":
```
<?php

//Include database configuration file
include('dbConfig.php');

//Create where condition depending on continent selected
if($_POST['continentIDs']!=""){
    $rest = substr($_POST['continentIDs'], 0, -1);
    $newContinentIDs = str_replace(","," OR country_continent_id=",$rest);
    $whereCondition = '(country_continent_id = ' . $newContinentIDs . ')';
}else{
    $whereCondition = 'country_continent_id = 0';
}

$queryCountry = $db->query("SELECT * FROM country ORDER BY country_name ASC " . $whereCondition);
echo json_encode( array(
  "data" => $queryCountry->fetch_all()
) );
<?php > ``` ?>

When I try to retrieve those IDs with $_POST['continentIDs'], I have the error:
"( ! ) Notice: Undefined index: continentIDs in D:\one-world\c_country.php on line 7"

What am I missing?

This question has an accepted answers - jump to answer

Answers

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    In your PHP script, you can put a var_dump($_POST); or a print_r($_POST); to see what are the real contents of $_POST.

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    Using var_dump($_POST);, I have:

    D:\one-world\c_country.php:14:
    array (size=0)
    empty

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17
    edited October 2017

    If you are using a browser like Chrome or FireFox you can see what your Javascript is actually sending to your PHP script. Use the developer tools for this and select Network. In the XHR of this Network Tools, you can see all that is being transferred between your Javascript and PHP. Then you will see what you need to change.

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    In the developer tool, I have:

    D:\one-world\c_country.php:14:
    array (size=1)
      'continentIDs' => string '2,6,1,3,5,4,' (length=12)
    
    
    

    {"data":[["8","Argentina","4"],["13","Australia","5"],["9","Bolivia","4"],["6","Canada","3"],["7","Chile","4"],["12","Colombia","4"],["11","Ecuador","4"],["16","India","6"],["2","Italy","1"],["3","Kenya","2"],["14","Mongolia","6"],["15","Nepal","6"],["1","Norway","1"],["10","Peru","4"],["5","Tanzania","2"],["4","Uganda","2"],["17","United States","3"]]}

    So my parameter "continentIDs" is in there. Now I'm really confused

  • allanallan Posts: 63,467Questions: 1Answers: 10,466 Site admin

    Weird - that does look like it should work. Is it possible you are modifying the $_POST variable in the script somewhere? Does POST work on any of your other tables?

    Can you link to the page so I can just double check it is being sent correctly? It would be shown in the "headers" tab of the Ajax request in the network inspector panel.

    Allan

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    Here is a live version:
    http://one-world.comeze.com/c_test.php

    The thing that adds to my confusion is that I pretty much copied that part of the code from the server-side version of the same webpage and it worked just fine there:
    http://one-world.comeze.com/m_test.php

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    To answer your questions, it's always possible that I'm modifying the $_POST variable somewhere by accident but I seriously doubt it since the code I copied on the first post of this conversation is pretty much it, this page is still very basic. And on that page, this is the first table I'm using POST. I didn't get to the next tables yet

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    I looked at the headers in developer tools, and I do see:
    continentIDs:2,6,1,3,5,4,

  • allanallan Posts: 63,467Questions: 1Answers: 10,466 Site admin
    Answer ✓

    http://one-world.comeze.com/c_test.php

    Appears to work.

    array(1) {
      ["continentIDs"]=>
      string(0) ""
    }
    {"data":[["8","Argentina","4"],["13","Australia","5"],["9","Bolivia","4"],["6","Canada","3"],["7","Chile","4"],["12","Colombia","4"],["11","Ecuador","4"],["16","India","6"],["2","Italy","1"],["3","Kenya","2"],["14","Mongolia","6"],["15","Nepal","6"],["1","Norway","1"],["10","Peru","4"],["5","Tanzania","2"],["4","Uganda","2"],["17","United States","3"]]}
    

    is what is being sent back by the server-side. Remove the var_dump and it would be valid JSON. The continentIDs parameter is there in the $_POST variable.

    Allan

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    Indeed.
    I guess I "fixed" the error somewhere along the troubleshooting and the error coming from var_dump (that was used for troubleshooting) got me confused. Ironic.

    Thank you Tester2017 and thank you Allan

  • dudaneskdudanesk Posts: 21Questions: 5Answers: 0

    I can see a little clearer now. It seems that the error I had never had anything to do with POST, but with my SQL query. For some reason, in the developer tools, when I was double-clicking on my php script to see the error message, it'd display:

    "( ! ) Notice: Undefined index: continentIDs in D:\one-world\c_country.php on line 7"
    (line 7 being: "if($_POST['continentIDs']!=""){"). I would then assume that POST wasn't getting my parameter...

    Once the SQL query fixed, the error message is gone and the code works fine.

This discussion has been closed.