1

Trying to populate a datatable using ajax. Passing the parameter as follows:

$('#submitDetails').on('click', function()
{
  let details = $('#searchDetails').val();

  $.ajax({
        url: 'api/searchDetailInfo.php',
        type: 'POST',
        data: details,
        dataType: 'html',
        success: function(data, textStatus, jqXHR){
          console.log(data); // <-- printing the return from the php script
          let jsonObject = JSON.parse(data);
          var table = $('#example1').DataTable({    
                "data": jsonObject,
                "columns": [
                        { "data": "COLUMN1" },              
                        { "data": "COLUMN2" },
                        { "data": "COLUMN3" }
                ],
                "iDisplayLength": 50,
                "order": [[ 1, "desc" ]],
                "paging": true,
                "scrollY": 550,
                "scrollX": true,
                "bDestroy": true,
                "stateSave": true,
                "sPaginationType":"full_numbers",
                "autoWidth": true
        },
        error: function(jqHHR, textStatus, errorThrown) {
            $('#loadingDiv').hide();
            $('#errorModal').modal('show');
            $('.message').text('There was an error conducting your search.');
            console.log('fail: '+ errorThrown);
            return false;
        }
});

I am getting to the PHP script, but the script is not receiving the parameter.

<?php
  include("../include/database.php");

  if(isset($_POST['details']))
  {
    echo "good";
  }
  else
  {
    echo "bad";
  }
?>

I'm only getting "bad" in the console. I am not sure why the parameter is not being sent to the script.

How can I fix this?

7
  • 2
    Have you tried print_r($_POST); to see what is sent. Commented May 28, 2020 at 18:24
  • @NigelRen - testing... Commented May 28, 2020 at 18:24
  • I get the following: Array ( [new_york] => ) Commented May 28, 2020 at 18:28
  • 2
    change data: details, to data: {details:details}, giving the post variable an identifier. Commented May 28, 2020 at 18:29
  • 1
    Done @JohnBeasley glad to help! Commented May 28, 2020 at 18:36

1 Answer 1

1

You need to change data: details, to data: {details:details}, giving the post variable an identifier, making $_POST['details'] valid.

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

Comments

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.