0

I have been struggling with it for a week...I want to use Ajax in datatables to pass some parameters to my PHP file for server side searching. But it seems my PHP file got nothing. But the datatable works just fine and the table can return from sever side processing and display on the html page correctly. However, var_dump( $_POST['species']) returns null.

  $(document).ready(function() {
    console.log(species);
    $("#table").DataTable({
      "destroy": true,
      "serverSide": true,
      "processing": true,
      "paging": false,
      "searching": false,

      columns: [{
          data: "d"
        },
        {
          data: "c"
        },
        {
          data: "p"
        }
      ],
      ajax: {
        type: "POST",
        data: {
          "species": s
        },
        url: "./php/search_test.php"
      }

    });
  });
<div class="container text-center" id="tablecontainer" name="tablecontainer">
  <table id="table" class="display nowrap row-border strip p-2" cellspacing="0" width="100%">

    <thead class="thead-dark">
      <tr>
        <th>
          <center>d</center>
        </th>
        <th>
          <center>C</center>
        </th>
        <th>
          <center>P</center>
        </th>

      </tr>
    </thead>
  </table>
</div>

'''php

ini_set ('memory_limit',  '2048M');
$sql_details = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db'   => 'xxxxx'
);

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

//if I try this here: $s=$_POST["s"]; echo $s;It //returns: Warning: Undefined array key "s".

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

// column
$columns = array(
array( 'db' => 'd', 'dt' => 'd' ),
array( 'db' => 'c', 'dt' => 'c' ),
array( 'db' => 'p',  'dt' => 'p' ),


);

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

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

Could someone help me with it? Thanks.
7
  • check the browser developer tools network tab to see what is being actually posted to PHP ... since your code does not show what species even is (no declaration at all), it's hard to tell what you're doing wrong - it's also interesting to note that the PHP you posted make no reference to species anyway, so, not sure what you think anyone could do for you with the absolute lack of information regarding your so called issue Commented May 2, 2022 at 5:35
  • Sorry I didn't make the question clear. I am new to stackflow and also programming. I will edit my question to make it clear. Commented May 2, 2022 at 10:58
  • Is species declared before you try to use it in {"species":species}? Commented May 2, 2022 at 11:01
  • I am sorry somehow I cannot eidt my question. Yes. I declared species in other parts of the code but it's too long so I didn't copy it here. If I console typeof('species'), it shows it is a string. Commented May 2, 2022 at 11:10
  • Click on the Edit link to edit your question. You can find it at the bottom of the question, underneath the question's tags (see Share Edit Close Delete) Commented May 2, 2022 at 14:21

1 Answer 1

0

Try this. Replace $_POST to $array

$array = json_decode(file_get_contents('php://input'), true); print_r($array);

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but nothig returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.