Passing parameter to server_processing.php using a

Passing parameter to server_processing.php using a

emmaranemmaran Posts: 4Questions: 1Answers: 0

Hi there,
I'm using datatables to display some record from my database (I modified server_processing.php to read with $_GET a parameter in query string). Everything works like a charm, but I've got one question. I would like to pass a dynamic parameter read from a select to server_processing.php to execute the query.
Thanks. Giacomo

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin

    Use the ajax.data option to pass extra data to the server.

    Allan

  • emmaranemmaran Posts: 4Questions: 1Answers: 0
    edited May 2014

    Ok, I'm trying. Here my datatables init:

    $(document).ready(function() {
    $('#example').dataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "scripts/server_processing.php",
    "data": {
    "id_professionista": 17
    }
    } );
    } );

    Now I'm trying to get id_professionista value in server_processing.php with $_GET['id_professionista'] method, but It doesn't work!
    How can i get it to work?
    Thank you, for your help.

    Giacomo

  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    Answer ✓

    You aren't using ajax.data. See the examples in the documentation. For example:

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "data": {
            "user_id": 451
        }
      }
    } );
    

    Allan

  • emmaranemmaran Posts: 4Questions: 1Answers: 0

    Thank you, so much!
    I finally got it to work.

    $(document).ready(function() {
    $('#example').dataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
    "url": "scripts/server_processing.php",
    "data": {
    "id_professionista": 17
    }
    }
    });
    } );

    Giacomo.

  • emmaranemmaran Posts: 4Questions: 1Answers: 0
    edited May 2014

    Now I would like to change dynamically id_professionisti value, reading this from a select box. Every time user selects a row in a the select box I would like to change id_professionisti and reload datatable with new results taken from database. Any suggests?

    Giacomo.

    EDIT:

    I solved using this function:

    $('#selezione_centri').change(function() {
    var valore = this.value;
    var string = ('scripts/server_processing.php?id_professionista='+valore);
    table.ajax.url(string).load();
    table.reload();
    });

  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin

    If you have a look at the documentation (it is there to try and be useful...!) you will see that you are able to use ajax.data as a function, allowing you to dynamically calculate values.

    Allan

This discussion has been closed.