Start and Index not being read server side

Start and Index not being read server side

ewanlpewanlp Posts: 3Questions: 1Answers: 0

Hello,

First question here and recently started using DataTables.

I am having trouble with an ajax call to the server - I am getting the errors:

Notice: Undefined index: length in C:\xampp\htdocs\testing\fetch.php on line 37
Notice: Undefined index: start in C:\xampp\htdocs\testing\fetch.php on line 38
Notice: Undefined index: length in C:\xampp\htdocs\testing\fetch.php on line 38

I have provided a link to my github pages where the pertinent code resides:
https://github.com/ewanlp/BoardGameSite/blob/testing-luke/pages/players.php
https://github.com/ewanlp/BoardGameSite/blob/testing-luke/testing/fetch.php

Debugging has not shown me anything as far as I know, but here is link to that as well:
http://debug.datatables.net/uguzow

The page appears to work how it should - I am able to sort, order, and paginate successfully. This said, these notices are indicating that my query is not correct. This is because the length and start values are not being filled in my query. I find this odd because when I debug my code I get this:

Data source Server-side Ajax
Processing Server-side
Draws 1
Columns 3
Rows 10
Display start row 0
Display length 10

The values length and start are being returned with values to the server. I'm not sure if I'm interpreting this correctly, and if there is some clarification that anyone can provide, that would be much appreciated!

Thanks for any help.
Luke

Answers

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    Interesting.... Looking at the debugger output in the Table Tab go to "Initialization options" it looks like you have configured for an AJAX POST.

        "ajax": {
            "url": "../testing/fetch.php",
            "type": "POST"
        },
    

    Under "Server interaction" it shows a "GET request:

    Ajax HTTP method    GET
    

    Which would explain why you get those errors with this code:

    if($_POST["length"] != -1) {
        $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
    

    What I don't understand is why it appears the request was a GET not a POST. Maybe Allan can explain?

    Kevin

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    I fear that's a legacy bug in the debugger... (urgh). It does it on this example as well. I'll get that fixed.

    Could you add print_r( $_POST ); to the file at the very top (before the session start) and let me know what the server's response is (it will be invalid JSON, but that's not important).

    Allan

  • ewanlpewanlp Posts: 3Questions: 1Answers: 0

    Thank you both for your responses.

    When adding print_r($_POST); to the top of the fetch file, the server returns:

    Array ()

    When I preview the page in my Network tab, it returns:

    Array( [draw] => 1 [columns] => Array ( [0] => Array ( [data] => 0 [name] => [searchable] => true [orderable] => true [search] => Array ( [value] => [regex] => false ) ) [1] => Array ( [data] => 1 [name] => [searchable] => true [orderable] => false [search] => Array ( [value] => [regex] => false ) ) [2] => Array ( [data] => 2 [name] => [searchable] => true [orderable] => false [search] => Array ( [value] => [regex] => false ) ) ) [start] => 0 [length] => 10 [search] => Array ( [value] => [regex] => false ))

    Again, thank you for any help you can provide.
    Luke

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Hi Luke,

    So that looks good - start and length are being submitted, and the server is seeing them.

    So next put that print_r line on line 29 and see if they are still there. If not, then one of the included scripts is mucking around with $_POST.

    Allan

  • ewanlpewanlp Posts: 3Questions: 1Answers: 0

    Allan,
    Checked that out and it looks good there. I looked at all other included files and have posted links to them below. I'm not seeing anything that could be messing with POST but perhaps you might be able to spot it better.
    When I use print_r right before line 39 I'm still seeing that start and length are being correctly filled with the appropriate values.

    https://github.com/ewanlp/BoardGameSite/blob/testing-luke/ePHP/class.user.php
    https://github.com/ewanlp/BoardGameSite/blob/testing-luke/ePHP/dbconfig.php
    https://github.com/ewanlp/BoardGameSite/blob/testing-luke/testing/function.php

    Let me know if there's any other links I can provide, or somewhere else the POST request might be interfered with.

    Luke

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    This is weird. So if you have print_r( $_POST ) on line 39-40 it shows the start and length parameters in it? But it is also showing the error message:

    Notice: Undefined index: length in C:\xampp\htdocs\testing\fetch.php on line 37

    ?

    I'm not sure I can explain that!

    Allan

  • denny_kurniawandenny_kurniawan Posts: 8Questions: 0Answers: 0
    edited February 2019

    Hai there, I have the same problem. Any fix for this?

  • denny_kurniawandenny_kurniawan Posts: 8Questions: 0Answers: 0

    but when I echo $_POST I got this: {"draw":null,"recordsTotal":2,"recordsFiltered":2,"data":[]}

    and this is my ajax request:
    ```$(document).ready(function() {
    table = $('#table').DataTable({

      "processing": true, //Feature control the processing indicator.
      "serverSide": true, //Feature control DataTables' server-side processing mode.
      "order": [], //Initial no order.
    
      // Load data for the table's content from an Ajax source
      "ajax": {
          "url": "<?php echo base_url('Manage/get_all')?>",
          "type": "POST"
      },
    
      //Set column definition initialisation properties.
      "columnDefs": [
          {
              "targets": [ 0 ], //first column
              "orderable": false, //set not orderable
          },
          {
              "targets": [ -1 ], //last column
              "orderable": false, //set not orderable
          },
    
      ],
    

    });```

  • denny_kurniawandenny_kurniawan Posts: 8Questions: 0Answers: 0

    and this is inside my network tab, form-data :

    draw: 1
    columns[0][data]: 0
    columns[0][name]: 
    columns[0][searchable]: true
    columns[0][orderable]: false
    columns[0][search][value]: 
    columns[0][search][regex]: false
    columns[1][data]: 1
    columns[1][name]: 
    columns[1][searchable]: true
    columns[1][orderable]: true
    columns[1][search][value]: 
    columns[1][search][regex]: false
    columns[2][data]: 2
    columns[2][name]: 
    columns[2][searchable]: true
    columns[2][orderable]: true
    columns[2][search][value]: 
    columns[2][search][regex]: false
    columns[3][data]: 3
    columns[3][name]: 
    columns[3][searchable]: true
    columns[3][orderable]: true
    columns[3][search][value]: 
    columns[3][search][regex]: false
    columns[4][data]: 4
    columns[4][name]: 
    columns[4][searchable]: true
    columns[4][orderable]: true
    columns[4][search][value]: 
    columns[4][search][regex]: false
    columns[5][data]: 5
    columns[5][name]: 
    columns[5][searchable]: true
    columns[5][orderable]: true
    columns[5][search][value]: 
    columns[5][search][regex]: false
    columns[6][data]: 6
    columns[6][name]: 
    columns[6][searchable]: true
    columns[6][orderable]: false
    columns[6][search][value]: 
    columns[6][search][regex]: false
    start: 0
    length: 10
    search[value]: 
    search[regex]: false
    
This discussion has been closed.