Mike in IT
asked on
DataTables - 2 tables one renders and one does not.
I have a reporting website that I am working to implement DataTables formatting with server side processing. I have several reports that I'm trying to show. They all go through the same set of files to run them. I have a *ServerSide.php* file that gets the needed info and sends it to the server. Then I have a *FilterSort.class.php* file that actually processes the data and pulls from my MSSQL server and returns a JSON response to the client. Then I have a file that is included in the calling file (ReportPage.php) called *DataTableLinks.php* that has the DataTable initialization. Here they are:
ReportPage.php
ServerSide.php
FilterSort.class.php
DataTableLinks.php
The problem is that I call two different reports, one will render and show the table filled with data and the other only shows the header and footer and nothing else for the table. When I check the console is shows that the ServerSide.php fould not be found. Which I don't understand since I'm using the same files for both calls. The order is:
ReportPage.php ->
ServerSide.php ->
FilterSort.class.php ->
ReportPage.php
ReportPage.php
<?php
include('Helper/PageName.php');
?>
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<title><?php echo $Title . " " . substr($_SERVER['COMPUTERNAME'],strlen($_SERVER['COMPUTERNAME'])-1,1); ?></title>
<?php require 'StyleLinks.php'; ?>
<?php include 'DataTableLinks.php'; include'array_column.php'; ?>
</head>
<body>
<?php
include 'Menu.php';
?>
<br><br><br>
<h1><?php echo $HeadingDesc; if(strpos($_SERVER['PHP_SELF'],"/",1)>0){echo "<br>Dev Site";} ?></h1>
<?php
include 'DBConn.php';
if ($Page == 'AdminPage')
{
echo "<script>location.href = 'http://engsys.corp.ftr.com/dev/FrontierReports/AdminPage.php?PageName=AdminPage'</script>";
}
$hsql = "select Headings from TableHeadings where TableName = '$TableName' and Headings != 'Edit' order by Id";
$getHeadings = $conn->query($hsql);
$rHeadings = $getHeadings->fetchALL(PDO::FETCH_ASSOC);
$CountHeadings = count($rHeadings);
$tsqlHeadings = '';
for ($row = 0; $row < $CountHeadings; $row++)
{
$headings[$row] = $rHeadings[$row]["Headings"];
}
?>
<table id="DataTable" class="display nowrap" style="width: 100%; border: 1px">
<thead>
<tr>
<?php
foreach($headings as $heading)
{?>
<th class="cell"><?php echo $heading; ?></th><?php
}?>
</tr>
</thead>
<tfoot>
<tr>
<?php
foreach($headings as $heading)
{?>
<th class="cell"><?php echo $heading; ?></th><?php
}?>
</tr>
</tfoot>
</table>
<?php //} ?>
<table class="NormalTable">
<tr>
<td>
<a class="Help" id="Help" href="http://engsys.corp.ftr.com/dev/Frontier/HelpPage.php" onclick="javascript:void window.open('http://engsys.corp.ftr.com/dev/FrontierReports/helppage.php','1472831666662','width=825,height=280,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');return false;">Help</a>
</td>
</tr>
</table>
</body>
</html>
ServerSide.php
<?php
include 'Helper/PageName.php'; //Pulls the page name and Table name and returns the $SQLTableName, $TableName, $Title and $HeadingDesc
include 'DBConn.php';
$headings = array(); //Create the empty array for use later and so that it won't throw an error if not assinged later
$hsql = "select Headings from TableHeadings where TableName = '$TableName' order by Id"; //Get all the column headers from the TableHeadings table in SQL
$getHeadings = $conn->query($hsql);
$rHeadings = $getHeadings->fetchALL(PDO::FETCH_ASSOC);
$CountHeadings = count($rHeadings); //Count how many columns that there will be
$tsqlHeadings = '';
$ColumnHeader = array();
for ($row = 0; $row < $CountHeadings; $row++)
{
if($rHeadings[$row]["Headings"] <> "Edit")
{
$headings[$row] = $rHeadings[$row]["Headings"]; //fill the array of column headings for use in creating the DataTable
}
}
foreach($headings as $index => $columnName)
{
$ColumnHeader[] = array('db'=>$columnName,'dt'=>$index); //creates the array for creating the DataTable column headers when sent to the FilterSort.class
}
//DB table to use
$table = $SQLTableName;
//Table's primary key
$primaryKey = 'id';
// Get data to display
$request = array();
$request = $_GET;
//print_r($_GET);
require('FilterSort.class.php');
echo json_encode(FilterSort::complex($request,$sqlConnect,$table,$primaryKey,$ColumnHeader));//,$whereResult,$whereAll));
?>
FilterSort.class.php
<?php
class FilterSort
{
static function data_output($columns,$data)
{
$out = array();
for($i=0,$ien=count($data);$i<$ien;$i++)
{
$row = array();
for($j=0,$jen=count($columns);$j<$jen;$j++)
{
$column = $columns[$j];
if(isset($column['Formatter']))
{
$row[$column['dt']] = $column['Formatter']($data[$i][$column['db']],$data[$i]);
}
else
{
$row[$column['dt']] = $data[$i][$columns[$j]['db']];
}
}
$out[] = $row;
}
return $out;
}
//Database connection
static function db ($conn)
{
if(is_array($conn))
{
return self::sql_connect($conn);
}
return $conn;
}
//Pagination
static function limit ($request,$columns)
{
$limit = '';
if(isset($request['start']) && $request['length'] != -1)
{
$limit = " OFFSET " . intval($request['start']) . " ROWS FETCH NEXT " . intval($request['length']) . " ROWS ONLY ";
}
return $limit;
}
//Ordering
static function order ($request,$columns)
{
$order = '';
if(isset($request['order']) && count($request['order']))
{
$orderBy = array();
$dtColumns = self::pluck($columns,'dt');
for($i=0,$ien=count($request['order']);$i<$ien;$i++)
{
//convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search($requestColumn['data'],$dtColumns);
$column = $columns[$columnIdx];
if($requestColumn['orderable'] == 'true')
{
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC':
'DESC';
$orderBy[] = $column['db']." ".$dir;
}
}
$order = ' ORDER BY '.implode(', ',$orderBy);
}
return $order;
}
// Searching / Filtering
// Construct the WHERE clause for server-side processing SQL query.
static function filter ( $request, $columns, &$bindings )
{
$globalSearch = array();
$columnSearch = array();
$dtColumns = self::pluck( $columns, 'dt' );
if (isset($request['search']) && $request['search']['value'] != '')
{
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']); $i<$ien; $i++ )
{
$requestColumn = $request['columns'][$i];
$columnIdx = array_search($requestColumn['data'], $dtColumns);
$column = $columns[ $columnIdx ];
if ($requestColumn['searchable'] == 'true')
{
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "'".$column['db']."' LIKE ".$binding;
}
}
}
// Individual column filtering
if ( isset( $request['columns'] ) )
{
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ )
{
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' &&
$str != '' )
{
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = $column['db']." LIKE ".$binding;
}
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) )
{
$where = ' ('.implode(' OR ', $globalSearch).') ';
}
if ( count( $columnSearch ) )
{
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' )
{
$where = ' WHERE '.$where;
}
return $where;
}
// You can apply additional 'where' conditions to the SQL queries
static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=NULL, $whereAll=NULL )
{
$bindings = array();
$db = self::db( $conn );
$localWhereResult = array();
$localWhereAll = array();
$whereAllSql = '';
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
$whereResult = self::_flatten( $whereResult );
$whereAll = self::_flatten( $whereAll );
if ( $whereResult )
{
$where = $where ? $where .' AND '.$whereResult : ' WHERE '.$whereResult;
}
if ( $whereAll )
{
$where = $where ? $where .' AND '.$whereAll : ' WHERE '.$whereAll;
$whereAllSql = ' WHERE '.$whereAll;
}
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,"SELECT ".implode(", ", self::pluck($columns, 'db'))." FROM $table$where$order$limit");
// Data set length after filtering
$resFilterLength = self::sql_exec( $db, $bindings,"SELECT COUNT({$primaryKey}) FROM $table $where" );
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db, "", "SELECT COUNT({$primaryKey}) FROM $table ".$whereAllSql);
$recordsTotal = $resTotalLength[0][0];
// Output
return array(
"draw" => isset ( $request['draw'] ) ?
intval( $request['draw'] ) :
0,
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
// Connect to the database
static function sql_connect ( $sql_details )
{
try {
$servername = $sql_details['servername'];
$username = $sql_details['username'];
$password = $sql_details['password'];
$dbname = $sql_details['dbname'];
$db = @new PDO("sqlsrv:server=$servername;database=$dbname", $username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 10);
}
catch (PDOException $e)
{
self::fatal("An error occurred while connecting to the database. The error reported by the server was: ".$e->getMessage());
}
return $db;
}
// Execute an SQL query on the database
static function sql_exec ( $db, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null )
{
$sql = $bindings;
$Newsql = $sql;
}
$stmt = $db->prepare( $sql );
// Bind parameters
if ( is_array( $bindings ) )
{
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ )
{
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try
{
$stmt->execute();
}
catch (PDOException $e)
{
self::fatal( "A SQL error has occurred: ".$e->getMessage());
}
// Return all
return $stmt->fetchAll( PDO::FETCH_BOTH );
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Internal methods
// Throw a fatal error.
static function fatal ( $msg )
{
echo json_encode(array("error" => $msg));
exit(0);
}
// Create a PDO binding key which can be used for escaping variables safely when executing a query with execute()
static function bind ( &$a, $val, $type)
{
$key = ':binding_'.count( $a );
$a[] = array(
'key' => $key,
'val' => $val,
'type' => $type
);
return $key;
}
// Pull a particular property from each assoc. array in a numeric array, returning and array of the property values from each item.
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ )
{
$out[] = $a[$i][$prop];
}
return $out;
}
// Return a string from an array or a string
static function _flatten ($a, $join = ' AND ')
{
if (!$a)
{
return '';
}
else if ($a && is_array($a))
{
return implode($join, $a);
}
return $a;
}
}
?>
DataTableLinks.php
<?php
echo '
<link rel="stylesheet" type="text/css" href="DataTables/dataTables.css"/>
<script type="text/javascript" src="DataTables/dataTables.js"></script>
';
?>
<script type="text/javascript" class="init">
$(document).ready(function () {
// Setup - add a text input to each footer cell
$('#DataTable tfoot th').each(function () {
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});
var table = $('#DataTable').DataTable({
"lengthMenu" : [[25, 50, 75, 100, 150], [25, 50, 75, 100, 150]],
"ScrollX" : true,
"dom" : '<"top"Bilp<"clear">>rt<"bottom"ip<"clear">>',
"buttons" : [{
extend : 'collection',
text : 'Selection',
buttons : ['selectAll', 'selectNone']
}, {
extend : 'collection',
text : 'Export',
buttons : ['excel', 'csv', 'pdf']
}
],
"fixedHeader" : {
header : true,
footer : true
},
"select" : true,
"processing" : true,
"serverSide" : true,
"ajax" : {
"url" : "./ServerSide.php?PageName=<?php echo $Page; ?>"
},
initComplete: function() {
var api = this.api();
// Apply the search
api.columns().every(function() {
var that = this;
$('input', this.footer()).on('keyup change', function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
}
});
});
</script>
The problem is that I call two different reports, one will render and show the table filled with data and the other only shows the header and footer and nothing else for the table. When I check the console is shows that the ServerSide.php fould not be found. Which I don't understand since I'm using the same files for both calls. The order is:
ReportPage.php ->
ServerSide.php ->
FilterSort.class.php ->
ReportPage.php
DataTableLinks.php makes reference to '#DataTable', which is the ID of an HTML element. You can't have more than one HTML element with the same ID on the same page. ID's must be unique. You can use a class to initialize '.DataTable' (all HTML elements with the DataTable class), but you may still need a way to identify each individual datatable. In the past, I've resolved this type of delimma by pushing each datatable into an array, and then I could reference the array index to access each individual datatable.
ASKER
There is only one DataTable table on the page. The only other table has an ID ="NormalTable". I won't be showing more than one report at a time. The problem is that I can open one report, then when I try to open a different one it doesn't render. I have to select it from my menu and it should send new data to the ServerSide.php file, but simply doesn't, claiming that the file cannot be found.
When I select one report it is the only report that is on the page so there's never more than one table that has the ID=DataTable
Hope that makes more sense.
When I select one report it is the only report that is on the page so there's never more than one table that has the ID=DataTable
Hope that makes more sense.
That does make more sense. You'll want to consider destroying a datatable before loading a new one. But I don't think that's your problem if you're receiving a file not found error.
What error do you see in F12 developer tools (network tab)? Does it show that ServerSide.php is not found?
What error do you see in F12 developer tools (network tab)? Does it show that ServerSide.php is not found?
ASKER
In dev tools it shows 404 file not found
So, let me see if I understand... you have a drop down menu with options. You select option 1 and see the report. You select option 2 and get a file not found. And all files are in the same directory.
If that's a correct understanding, could you please show us the function that gets triggered when the option is selected from the drop down?
If that's a correct understanding, could you please show us the function that gets triggered when the option is selected from the drop down?
ASKER
You are understanding correctly. I am using 2 reports as examples (Qdefs ad Approved Projects). It doesn't matter the order that I try running them, or if I restart the browser or try multiple browsers, Qdefs works and Approved Projects does not. The only difference in the way that they are called is the ?PageName= on the end of the URL. I've added some screenshots to show this.
So here's what happens.
You click on the menu item and you are taken to the ReportPage.php
At the same time the jquery is calling ServerSide.php
Here's the jquery from DataTableLinks.php
Then after calling ServerSide.php it should go to FilterSort.class.php which is the server side file that creates the JSON that DataTables uses to create the table.
Then it's sent back to ReportPage.php where it is formatted and put into the table based on the parameters that I have in the above code.
ApprovedProjects.JPG
QDEF.JPG
Files.JPG
So here's what happens.
You click on the menu item and you are taken to the ReportPage.php
This is where the DataTable is created with a header and footer
Then the jquery from the DataTableLinks.php kicks in and should attempt to format the tableAt the same time the jquery is calling ServerSide.php
This is when the problems begin, the Qdefs report calls it just fine while the Approved Projects report does not.
I've attached a screen shot showing that the two files (ReportPage.php and ServerSide.php are in the same directory.Here's the jquery from DataTableLinks.php
<script type="text/javascript" class="init">
$(document).ready(function () {
// Setup - add a text input to each footer cell
$('#DataTable tfoot th').each(function () {
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});
var table = $('#DataTable').DataTable({
"lengthMenu" : [[25, 50, 75, 100, 150], [25, 50, 75, 100, 150]],
"ScrollX" : true,
"dom" : '<"top"Bilp<"clear">>rt<"bottom"ip<"clear">>',
"buttons" : [{
extend : 'collection',
text : 'Selection',
buttons : ['selectAll', 'selectNone']
}, {
extend : 'collection',
text : 'Export',
buttons : ['excel', 'csv', 'pdf']
}
],
"fixedHeader" : {
header : true,
footer : true
},
"select" : true,
"processing" : true,
"serverSide" : true,
"ajax" : {
"url" : "./ServerSide.php?PageName=<?php echo $Page; ?>"
},
initComplete: function() {
var api = this.api();
// Apply the search
api.columns().every(function() {
var that = this;
$('input', this.footer()).on('keyup change', function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
}
});
});
</script>
Then after calling ServerSide.php it should go to FilterSort.class.php which is the server side file that creates the JSON that DataTables uses to create the table.
Then it's sent back to ReportPage.php where it is formatted and put into the table based on the parameters that I have in the above code.
ApprovedProjects.JPG
QDEF.JPG
Files.JPG
From F12 dev tools, on the network tab, copy the url that's being attempted. Paste that in a new browser and see if you can access the page.
Can you also post the url that works and the url that does not work here in this thread?
Usually, these 404 type errors are spot-on and there is a problem with the path / url or file location.
Can you also post the url that works and the url that does not work here in this thread?
Usually, these 404 type errors are spot-on and there is a problem with the path / url or file location.
ASKER
i would think that the error was spot on as well if it wasn't for both calls being the same. They both use the same files there's no difference in that.
This is an internal site so you won't be able to navigate to it.
I have tried copying and pasteing into another tab or into another browser. Still doesn't work.
Here is the url that works:
Here's the url that doesn't work:
This is an internal site so you won't be able to navigate to it.
I have tried copying and pasteing into another tab or into another browser. Still doesn't work.
Here is the url that works:
http://engsys.corp.ftr.com/dev/FrontierReports/ServerSide.php?PageName=QDefs&draw=1&columns[0][data]=0&columns[0][name]=&column s[0][searc hable]=tru e&columns[ 0][orderab le]=true&c olumns[0][ search][va lue]=&colu mns[0][sea rch][regex ]=false&co lumns[1][d ata]=1&col umns[1][na me]=&colum ns[1][sear chable]=tr ue&columns [1][ordera ble]=true& columns[1] [search][v alue]=&col umns[1][se arch][rege x]=false&c olumns[2][ data]=2&co lumns[2][n ame]=&colu mns[2][sea rchable]=t rue&column s[2][order able]=true &columns[2 ][search][ value]=&co lumns[2][s earch][reg ex]=false& columns[3] [data]=3&c olumns[3][ name]=&col umns[3][se archable]= true&colum ns[3][orde rable]=tru e&columns[ 3][search] [value]=&c olumns[3][ search][re gex]=false &columns[4 ][data]=4& columns[4] [name]=&co lumns[4][s earchable] =true&colu mns[4][ord erable]=tr ue&columns [4][search ][value]=& columns[4] [search][r egex]=fals e&columns[ 5][data]=5 &columns[5 ][name]=&c olumns[5][ searchable ]=true&col umns[5][or derable]=t rue&column s[5][searc h][value]= &columns[5 ][search][ regex]=fal se&columns [6][data]= 6&columns[ 6][name]=& columns[6] [searchabl e]=true&co lumns[6][o rderable]= true&colum ns[6][sear ch][value] =&columns[ 6][search] [regex]=fa lse&column s[7][data] =7&columns [7][name]= &columns[7 ][searchab le]=true&c olumns[7][ orderable] =true&colu mns[7][sea rch][value ]=&columns [7][search ][regex]=f alse&order [0][column ]=0&order[ 0][dir]=as c&start=0& length=25& search[val ue]=&searc h[regex]=f alse&_=148 1043431145
Here's the url that doesn't work:
http://engsys.corp.ftr.com/dev/FrontierReports/ServerSide.php?PageName=ApprovedProjects&draw=1&columns[0][data]=0&columns[0][name]=&column s[0][searc hable]=tru e&columns[ 0][orderab le]=true&c olumns[0][ search][va lue]=&colu mns[0][sea rch][regex ]=false&co lumns[1][d ata]=1&col umns[1][na me]=&colum ns[1][sear chable]=tr ue&columns [1][ordera ble]=true& columns[1] [search][v alue]=&col umns[1][se arch][rege x]=false&c olumns[2][ data]=2&co lumns[2][n ame]=&colu mns[2][sea rchable]=t rue&column s[2][order able]=true &columns[2 ][search][ value]=&co lumns[2][s earch][reg ex]=false& columns[3] [data]=3&c olumns[3][ name]=&col umns[3][se archable]= true&colum ns[3][orde rable]=tru e&columns[ 3][search] [value]=&c olumns[3][ search][re gex]=false &columns[4 ][data]=4& columns[4] [name]=&co lumns[4][s earchable] =true&colu mns[4][ord erable]=tr ue&columns [4][search ][value]=& columns[4] [search][r egex]=fals e&columns[ 5][data]=5 &columns[5 ][name]=&c olumns[5][ searchable ]=true&col umns[5][or derable]=t rue&column s[5][searc h][value]= &columns[5 ][search][ regex]=fal se&columns [6][data]= 6&columns[ 6][name]=& columns[6] [searchabl e]=true&co lumns[6][o rderable]= true&colum ns[6][sear ch][value] =&columns[ 6][search] [regex]=fa lse&column s[7][data] =7&columns [7][name]= &columns[7 ][searchab le]=true&c olumns[7][ orderable] =true&colu mns[7][sea rch][value ]=&columns [7][search ][regex]=f alse&colum ns[8][data ]=8&column s[8][name] =&columns[ 8][searcha ble]=true& columns[8] [orderable ]=true&col umns[8][se arch][valu e]=&column s[8][searc h][regex]= false&colu mns[9][dat a]=9&colum ns[9][name ]=&columns [9][search able]=true &columns[9 ][orderabl e]=true&co lumns[9][s earch][val ue]=&colum ns[9][sear ch][regex] =false&col umns[10][d ata]=10&co lumns[10][ name]=&col umns[10][s earchable] =true&colu mns[10][or derable]=t rue&column s[10][sear ch][value] =&columns[ 10][search ][regex]=f alse&colum ns[11][dat a]=11&colu mns[11][na me]=&colum ns[11][sea rchable]=t rue&column s[11][orde rable]=tru e&columns[ 11][search ][value]=& columns[11 ][search][ regex]=fal se&columns [12][data] =12&column s[12][name ]=&columns [12][searc hable]=tru e&columns[ 12][ordera ble]=true& columns[12 ][search][ value]=&co lumns[12][ search][re gex]=false &columns[1 3][data]=1 3&columns[ 13][name]= &columns[1 3][searcha ble]=true& columns[13 ][orderabl e]=true&co lumns[13][ search][va lue]=&colu mns[13][se arch][rege x]=false&c olumns[14] [data]=14& columns[14 ][name]=&c olumns[14] [searchabl e]=true&co lumns[14][ orderable] =true&colu mns[14][se arch][valu e]=&column s[14][sear ch][regex] =false&col umns[15][d ata]=15&co lumns[15][ name]=&col umns[15][s earchable] =true&colu mns[15][or derable]=t rue&column s[15][sear ch][value] =&columns[ 15][search ][regex]=f alse&colum ns[16][dat a]=16&colu mns[16][na me]=&colum ns[16][sea rchable]=t rue&column s[16][orde rable]=tru e&columns[ 16][search ][value]=& columns[16 ][search][ regex]=fal se&columns [17][data] =17&column s[17][name ]=&columns [17][searc hable]=tru e&columns[ 17][ordera ble]=true& columns[17 ][search][ value]=&co lumns[17][ search][re gex]=false &columns[1 8][data]=1 8&columns[ 18][name]= &columns[1 8][searcha ble]=true& columns[18 ][orderabl e]=true&co lumns[18][ search][va lue]=&colu mns[18][se arch][rege x]=false&c olumns[19] [data]=19& columns[19 ][name]=&c olumns[19] [searchabl e]=true&co lumns[19][ orderable] =true&colu mns[19][se arch][valu e]=&column s[19][sear ch][regex] =false&col umns[20][d ata]=20&co lumns[20][ name]=&col umns[20][s earchable] =true&colu mns[20][or derable]=t rue&column s[20][sear ch][value] =&columns[ 20][search ][regex]=f alse&colum ns[21][dat a]=21&colu mns[21][na me]=&colum ns[21][sea rchable]=t rue&column s[21][orde rable]=tru e&columns[ 21][search ][value]=& columns[21 ][search][ regex]=fal se&columns [22][data] =22&column s[22][name ]=&columns [22][searc hable]=tru e&columns[ 22][ordera ble]=true& columns[22 ][search][ value]=&co lumns[22][ search][re gex]=false &columns[2 3][data]=2 3&columns[ 23][name]= &columns[2 3][searcha ble]=true& columns[23 ][orderabl e]=true&co lumns[23][ search][va lue]=&colu mns[23][se arch][rege x]=false&c olumns[24] [data]=24& columns[24 ][name]=&c olumns[24] [searchabl e]=true&co lumns[24][ orderable] =true&colu mns[24][se arch][valu e]=&column s[24][sear ch][regex] =false&col umns[25][d ata]=25&co lumns[25][ name]=&col umns[25][s earchable] =true&colu mns[25][or derable]=t rue&column s[25][sear ch][value] =&columns[ 25][search ][regex]=f alse&colum ns[26][dat a]=26&colu mns[26][na me]=&colum ns[26][sea rchable]=t rue&column s[26][orde rable]=tru e&columns[ 26][search ][value]=& columns[26 ][search][ regex]=fal se&columns [27][data] =27&column s[27][name ]=&columns [27][searc hable]=tru e&columns[ 27][ordera ble]=true& columns[27 ][search][ value]=&co lumns[27][ search][re gex]=false &columns[2 8][data]=2 8&columns[ 28][name]= &columns[2 8][searcha ble]=true& columns[28 ][orderabl e]=true&co lumns[28][ search][va lue]=&colu mns[28][se arch][rege x]=false&c olumns[29] [data]=29& columns[29 ][name]=&c olumns[29] [searchabl e]=true&co lumns[29][ orderable] =true&colu mns[29][se arch][valu e]=&column s[29][sear ch][regex] =false&col umns[30][d ata]=30&co lumns[30][ name]=&col umns[30][s earchable] =true&colu mns[30][or derable]=t rue&column s[30][sear ch][value] =&columns[ 30][search ][regex]=f alse&colum ns[31][dat a]=31&colu mns[31][na me]=&colum ns[31][sea rchable]=t rue&column s[31][orde rable]=tru e&columns[ 31][search ][value]=& columns[31 ][search][ regex]=fal se&columns [32][data] =32&column s[32][name ]=&columns [32][searc hable]=tru e&columns[ 32][ordera ble]=true& columns[32 ][search][ value]=&co lumns[32][ search][re gex]=false &columns[3 3][data]=3 3&columns[ 33][name]= &columns[3 3][searcha ble]=true& columns[33 ][orderabl e]=true&co lumns[33][ search][va lue]=&colu mns[33][se arch][rege x]=false&c olumns[34] [data]=34& columns[34 ][name]=&c olumns[34] [searchabl e]=true&co lumns[34][ orderable] =true&colu mns[34][se arch][valu e]=&column s[34][sear ch][regex] =false&col umns[35][d ata]=35&co lumns[35][ name]=&col umns[35][s earchable] =true&colu mns[35][or derable]=t rue&column s[35][sear ch][value] =&columns[ 35][search ][regex]=f alse&colum ns[36][dat a]=36&colu mns[36][na me]=&colum ns[36][sea rchable]=t rue&column s[36][orde rable]=tru e&columns[ 36][search ][value]=& columns[36 ][search][ regex]=fal se&columns [37][data] =37&column s[37][name ]=&columns [37][searc hable]=tru e&columns[ 37][ordera ble]=true& columns[37 ][search][ value]=&co lumns[37][ search][re gex]=false &columns[3 8][data]=3 8&columns[ 38][name]= &columns[3 8][searcha ble]=true& columns[38 ][orderabl e]=true&co lumns[38][ search][va lue]=&colu mns[38][se arch][rege x]=false&o rder[0][co lumn]=0&or der[0][dir ]=asc&star t=0&length =25&search [value]=&s earch[rege x]=false&_ =148104835 4261
ASKER
I tried testing it again today with pasting the URL from the dev tools network tab. Now it when I do that it shows the JSON response on the page. Which is what that file is supposed to be doing, the ServerSide.php file that is.
So I know that the file works, it's just not found when being called from the other file for the Approved Projects report.
So I know that the file works, it's just not found when being called from the other file for the Approved Projects report.
Wow. That is a long url. With that much data, have you considered using POST instead of GET ? There is a limit on the number of characters in an url. However, I can't say whether or not that's causing your problem. The server should return an "URL too long" type of error instead of "Page Not Found". But it's certainly a design consideration you should review.
ASKER
I actually don't know exactly how it is sent to ServerSide.php since that never shows up in the address bar of the browser. I'm not sure how I'd send it through POST instead of GET, or if it's even going through GET.
This is how I'm initializing it now:
How would I change that so that it gets sent using POST?
This is how I'm initializing it now:
$(document).ready(function () {
//console.log("<?php //echo $_GET['PageName']; ?>");
// Setup - add a text input to each footer cell
$('#DataTable tfoot th').each(function () {
var title = $(this).text();
$(this).html('<input type="text" placeholder="Search ' + title + '" />');
});
var table = $('#DataTable').DataTable({
"lengthMenu" : [[25, 50, 75, 100, 150], [25, 50, 75, 100, 150]],
"ScrollX" : true,
"dom" : '<"top"Bilp<"clear">>rt<"bottom"ip<"clear">>',
"buttons" : [{
extend : 'collection',
text : 'Selection',
buttons : ['selectAll', 'selectNone']
}, {
extend : 'collection',
text : 'Export',
buttons : ['excel', 'csv', 'pdf']
}
],
"fixedHeader" : {
header : true,
footer : true
},
"select" : true,
"processing" : true,
"serverSide" : true,
"ajax" : {
"url" : "./ServerSide.php?PageName=<?php echo $Page; ?>"
},
initComplete: function() {
var api = this.api();
// Apply the search
api.columns().every(function() {
var that = this;
$('input', this.footer()).on('keyup change', function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
}
});
});
How would I change that so that it gets sent using POST?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@zephyr_hex dude you are a genius!
I've been trying to figure that out for weeks now! I changed it to POST and now it finds the file (and now I have other problems, but at least it finds the file)!
I've been trying to figure that out for weeks now! I changed it to POST and now it finds the file (and now I have other problems, but at least it finds the file)!
oh yay! glad that worked.
p.s. i'm a "dude-ette" ;)
p.s. i'm a "dude-ette" ;)
ASKER
Well, thanks for the help. I don't run into too many "dude-ettes" in the field. I do have one more question and since it has to do with the solution that you gave me I thought I'd ask here.
How can I send the PageName through POST?
I was doing it through GET because ServerSide.php is being called from the jquery when the DataTable is being initialized. I don't know enough yet about jquery personally to get the PageName added to the POST request.
How can I send the PageName through POST?
I was doing it through GET because ServerSide.php is being called from the jquery when the DataTable is being initialized. I don't know enough yet about jquery personally to get the PageName added to the POST request.
Yes, you can send PageName through POST. A common way to do something like this would be to add a hidden input with name = "PageName" to your form. Fill in the input with the page name when the drop down option is selected. That hidden field will get sent with POST just like any other field on the form.
ASKER
Again, thank you for your help. I did get this working for me. Though I think that I didn't adequately describe my menu. The menu consists of links, there is no form. I ended up adding to my code a little, though. I have a file called PageName.php where I set the page name based on the menu item that is selected and the link that is in it. It now looks like this:
My menu is a bunch of nested ul and li like this:
$Page = '';
if (isset($_GET['PageName']))
{
//echo "<br>Page = Get<br>";
$Page = $_GET['PageName'];
}
elseif (isset($_POST['PageName']))
{
//echo "<br>Page = Post<br>";
$Page = $_POST['PageName'];
}
if(!isset($Page) || $Page == "")
{
$Page = $_SERVER['HTTP_REFERER'];
$Page = str_replace('http://engsys.corp.ftr.com/dev/FrontierReports/ReportPage.php?PageName=','',$Page);
}
Now the $Page variable gets set every time and I've added it to POST in my Serverside.php file like this:$_POST['PageName'] = $Page;
My menu is a bunch of nested ul and li like this:
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix" id="menu">
<li><a href="index.php">Home</a></li>
<li><a href="OPR Reports.php">OPCEN Reports</a>
<ul class="sub-menu">
<li><a href="ReportPage.php?PageName=COEI_OPR">COEI OPR Report</a></li>
<li><a href="ReportPage.php?PageName=OSP_OPR">OSP OPR Report</a></li>
<li><a href="ReportPage.php?PageName=OPR_COEI">OPCEN COEI Report</a></li>
<li><a href="ReportPage.php?PageName=OPR_OSP">OPCEN OSP Report</a></li>
<li><a href="ReportPage.php?PageName=EOJ">EOJ Report</a></li>
<li><a href="MaterialTracking.php">Material Tracking</a>