Method or type POST not posting, returns GET

Method or type POST not posting, returns GET

AndreOosthuizenAndreOosthuizen Posts: 14Questions: 3Answers: 0
edited June 2020 in Free community support

This is my current code which I down-scaled to bare minimum to try and get ajax to post and not get -

var ims_data_table = $('#all_parts').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url" : "../includes/ims_table_process.php",
                "type" : "POST"
            }
        });

I have changed type to method, I have tried with and without quotes, double quotes and none. The url does get to my php page where I am dumping values. When I try $_GET, I receive a single localhost ID return value. When I try $_POST, my array is empty. When I looked in my Network, it shows that the Request Method is GET. There are many such posts but I have tried all of their fixes, still only receives a GET.

This obviously interferes with my data query which means nothing is returned.

Thank you again for assisting, much appreciated.

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

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    You can use the browser's Network Inspector tool with this Server Side Processing with POST example and see, in the Header tab, that a POST request is sent.

    What do you see with your page? If you see a GET request then we will need a link to your page or a test case replicating the issue to help debug.

    Kevin

  • AndreOosthuizenAndreOosthuizen Posts: 14Questions: 3Answers: 0

    Thank you for prompt reply Kevin, much appreciated.

    My apologies around the code block, will pay attention to this.

    As per my post, my browser Network shows Request Method: GET, I have checked with every change of code, it stayed on GET. Many solutions pointed me in this direction, hence my check there.

    How would I load a test case/what is required for a test case?

    Thank you.

  • AndreOosthuizenAndreOosthuizen Posts: 14Questions: 3Answers: 0
    edited June 2020

    @Kevin, I recreated the page with basic functions, please have a look - https://bid4anything.com/ims_test_page.php

    You will see that ims_test_page shows GET in browser network whilst ims_table_process shows POST.

    I also left a var_dump on values, you will see that nothing is dumped on POST. It will also throw the invalid Json error because half of code is removed.

    I hope this helps.

  • AndreOosthuizenAndreOosthuizen Posts: 14Questions: 3Answers: 0

    Further to the above, code I used -

       <DOCTYPE html>
       <html lang="en">
       <head>
        <!--All java, icon and font files-->
        <link rel="stylesheet" 
       href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
        <script type="text/javascript" 
       src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script 
       src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
        <link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' />
        <!--Datatables-->
        <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"> 
      </script>
        <link rel="stylesheet" 
       href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" />
       </head>
       <body>
        <div class="table-responsive">
            <table id="all_parts" class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Part Number</th>
                        <th>Part Name</th>
                        <th>Cost Price</th>
                        <th>Advertised Price</th>
                        <th>D.M.S. Retail</th>
                        <th>Days In Stock</th>
                        <th>Qauntity Stocked</th>
                        <th>Edit Stock</th>
                    </tr>
                </thead>
            </table>
        </div>
    

    ```
    <script type="text/javascript" language="javascript">
    $ = jQuery;

        var ims_data_table = $('#all_parts').DataTable({
            "lengthMenu": [
                [10, 1, 5, 10, 25, 50, 75, -1],
                [10, 1, 5, 10, 25, 50, 75, "ALL"]
            ],
                "processing": true,
                "serverSide": true,
                "ajax": {
                    "url" : "../includes/ims_table_process.php",
                    "type" : "POST"
                }
            });
    </script>
    

    </body>
    </html>
    ``````

    php - ims_table_process.php

    global $db;
    
    $query = "";
    $output = array();
    
    //Grab code from aging page, select correct database...
    $query .="SELECT * FROM table ";
    
    //var_dump("Testing POST values here - ".$_POST);
    var_dump($_POST['start']." testing length left, start right ".$_POST['length']);
    
    if (isset($_POST["search"]["value"])) {
        $query .= "WHERE (partnumber =  '".$_POST["search"]["value"]."') OR (partdescription LIKE '%'".$_POST["search"]["value"]."'%') AND dealer_id = '".$_SESSION['dealer_id']."' AND quantity > '0'" ;
    
    }
    
    if (isset($_POST["order"])) {
        $query .= "ORDER BY ".$_POST['order']['0']['column']." ".$_POST['order']['0']['dir']."";
    
        //var_dump($query." column order value posted");
    } else {
        $query .="ORDER BY agedays DESC ";
    
        //var_dump($query." no column order value posted");
    }
    
    if ($_POST['length'] != -1) {
        $query .= "LIMIT ".$_POST['start'].", ".$_POST['length']."";
    
        //var_dump($query." post length not -1 value posted");
    }
    
  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    You will see that ims_test_page shows GET in browser network whilst ims_table_process shows POST.

    Yes, that is expected that the browser will use GET for the URL typed into the browser. The "type" : "POST" won't affect this. It only controls the request type for the Ajax request which, as you mentioned, is POST.

    I might not understand your question but the initial request for ims_test_page.php is outside of Datatables control. Maybe you can find what you need on Stack Overflow maybe something like this thread.

    Kevin

  • AndreOosthuizenAndreOosthuizen Posts: 14Questions: 3Answers: 0
    edited June 2020

    Thank you again for reply,

    Sorry, I disagree, the test page has everything to do with the datatables as it hosts the table with all of its features. As per my original post, when I use type : post, it does not post but shows get in browser network. This causes that length and start is not posted and err to undefined index, to my table_process page where I can actually get the data and show it in the table on the test page.

    Please check the link above for clarification if you don't mind, thanks.

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918
    Answer ✓

    when I use type : post, it does not post but shows get in browser network.

    Your test page is using POST for the Ajax request to ims_table_process.php as configured:

    However note that this does not control the request type for fetching the page (ims_test_page.php) which is using request type of GET:

    You can see the request (ims_table_process.php) parameters in the Headers tab at the bottom. You can see it has the start, length and all the other parameters documented in the Server Side Processing docs:

    draw: 1
    columns[0][data]: 0
    columns[0][name]: 
    columns[0][searchable]: true
    columns[0][orderable]: true
    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]: true
    columns[6][search][value]: 
    columns[6][search][regex]: false
    columns[7][data]: 7
    columns[7][name]: 
    columns[7][searchable]: true
    columns[7][orderable]: true
    columns[7][search][value]: 
    columns[7][search][regex]: false
    columns[8][data]: 8
    columns[8][name]: 
    columns[8][searchable]: true
    columns[8][orderable]: true
    columns[8][search][value]: 
    columns[8][search][regex]: false
    order[0][column]: 0
    order[0][dir]: asc
    start: 0
    length: 10
    search[value]: 
    search[regex]: false
    

    This is the response, which is not a valid JSON strong:

    string(37) "0 testing length left, start right 10"
    []
    

    Your server side script is where the debugging needs to take place. The client side is working as expected.

    I'm don't use PHP but I see this in your script:

    var_dump($_POST['start']." testing length left, start right ".$_POST['length']);

    Looks like that is what you are returning: "0 testing length left, start right 10"

    Seems like the start and length values are there.

    I'm not sure why the response is as above instead of the records from your DB. Are you using Datatables provided server side scripts?

    Kevin

  • AndreOosthuizenAndreOosthuizen Posts: 14Questions: 3Answers: 0
    edited June 2020

    Hi Kevin,

    Thank you very much, I know now where to search for the fix. I was using server side scripts but it did not work for my needs so I did my own php, I know, re-inventing the wheel...

    The error seems to be created on this line of code - $query .= "LIMIT ".$_POST['start'].", ".$_POST['length']."";

    Thank you again, this is much appreciated. I learned something today, look in the header responses as well for clues!

    The string(37) part is just the var-dump to see what is returned by php, what is funny is that your has returned the values 0 and 10 "0 testing length left, start right 10", just tried on my side and nothing was returned, grrr. I will mark this as solved though as I know where to look though.

This discussion has been closed.