Another question regarding loading records from a variable

Another question regarding loading records from a variable

ConnectionProblemConnectionProblem Posts: 3Questions: 0Answers: 0
edited November 2012 in General
I'm sorry for posting this question again, but I searched and read many threads on using fnServerData with no avail. I am trying to only load a list of patients from a specific Dr's ID. The doctor field in the patients DB is populated with the Dr's ID and the session variable that I want to use is $id.

[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "removedPath"
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name" : "doctor", "value" : <?php $id;?> } );
$.getJSON( sSource, aoData, function (json) {
fnCallback(json);
});
},

} );
} );
[/code]


[code]// if id is specified, add it to the WHERE clause
if ( isset($_GET['doctor']) && $_GET['doctor'] != "" ) {
if ($sWhere) $sWhere .= " AND doctor=".mysql_real_escape_string( $_GET['doctor'] );
else $sWhere = " WHERE doctor=".mysql_real_escape_string( $_GET['doctor'] );
}[/code]

Replies

  • girishmrgirishmr Posts: 137Questions: 0Answers: 0
    edited November 2012
    Trying to load data using $_SESSION in a external java script won't work. Workaround would be

    1. To use the $_SESSION['id'] in you PHP script directly and not passing thru' aoData or
    2. To get the value of the ID field using ID or Class selectors and passing it to aoData

    If you are using the an inline script, you need to assign the session value to a variable and use that in aoData

    [code]
    var id = "<?php echo $_SESSION['id']; ?>"
    [/code]

    I see you are missing "echo" keyword in your assignment
    [code]
    aoData.push( { "name" : "doctor", "value" : "<?php echo $id; ?>" } );
    [/code]
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Does the correct information get sent to the server? What does the generated SQL look like? Does it have the additional WHERE condition in it? Can you link to the page please?

    I'd suggest you use fnServerParams rather than fnServerData since it is much easier (you don't need to make your own Ajax call).

    Allan
  • ConnectionProblemConnectionProblem Posts: 3Questions: 0Answers: 0
    Thank you both I ultimately used fnServerParams and made a js reference to the php variable.

    [code] "fnServerParams": function (aoData) { aoData.push({ "name": "doctor", "value": id }) },[/code]

    Thanks again guys.
This discussion has been closed.