I am not sure which part caused the page response nothing.These page is host in my lubuntu machine. However, when I access this page from another LAN computer,choosing the startDate to be '2012-12-16' and endDate to be '2013-12-16', the page just freeze in Chrome browser. Any idea when it is freeze? When I call $.get('duty.php'...
, the alert even cannot pop up. When I going stright to
/attendence/duty.php?mode=9&startDate=2012-12-06&endDate=2013-12-06&christened=All
the return is
[]
*EDIT 2 * Found the reason and it turns out to be another "silly" question.The problem was caused by $.get()
. As you can see the output from php is an empty array []
,which is ok. Yet the $.get()
function did not see it as a valid response and just wait until the browser timeout.(Using jQuery 1.7.2) The workaround is to return json with some content like {'no','result'}
.
Apache2 access.log
192.168.1.7 - - [06/Dec/2012:21:02:48 +0800] "GET /attendence/duty.php?mode=9&startDate=2012-12-06&endDate=2013-12-06&christened=All HTTP/1.1" 500 411 "-" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/11.10 Chromium/18.0.1025.168 Chrome/18.0.1025.168 Safari/535.19"
$.get('duty.php',
{'mode':9,
'startDate':$('input[name=startDate]').val(),
'endDate':$('input[name=endDate]').val(),
'christened':$('input[name=christened]:checked').val()},
function(data, textStatus, jqXHR){
alert('result reach back');
if (typeof data === 'undefined') {
return;
}
numWeek = getDiffWeek();
tableHtml='Num Of week:'+numWeek+'<table border=1><tr>';
tableHtml+='<td>Barcode</td><td>name</td><td>attendence</td><td>frequency</td>';
tableHtml+=' </tr></table>';
$('#attendenceRate').html(tableHtml);
for(name in data){
attendenceRate = Math.round(data[name]['times']/numWeek*100);
memberIcon ='';
$('#attendenceRate table').append('<tr><td>'+data[name]['barcode']+'</td><td>'+name+'</td><td>'+attendenceRate+'%</td><td>'+data[name]['times']+'</td></tr>');
}
}
,'json'
);
EDIT just a typo, should be duty.php instead of Duty.php
include ("lock.php");
if($_GET){
if ($_GET['mode']==9){//calculate overall christian attendence
$startDate = $_GET['startDate'];
$endDate = $_GET['endDate'];
$christened=$_GET['christened'];
if($christened=='All'){
$christenedClause='';
}else{
$christenedClause= ' AND record.christened = '.$christened;
}
$sql = <<<EOD
SELECT name,barcode, COUNT( * ) AS times ,christened,gender
FROM (
SELECT name,attendence.barcode as barcode, DATE, TIME,christened,gender
FROM attendence, record
WHERE attendence.barcode = record.barcode
AND DATE
BETWEEN "$startDate"
AND "$endDate"
$christenedClause
GROUP BY name, DATE
)A
GROUP BY name
EOD;
$result = $link->query($sql);
$data = array();
$i=0;
if($result->num_rows>0){
while ($result2 = $result->fetch_assoc()){
$data[$result2['name']]['times'] = $result2['times'];
$data[$result2['name']]['barcode'] = $result2['barcode'];
$data[$result2['name']]['gender'] = $result2['gender'];
$data[$result2['name']]['christened'] = $result2['christened'];
$i++;
}
}
echo json_encode($data);
}
}
Your page is throwing a Server Error. The 500
in your access log (All HTTP/1.1" 500 411
) means that the page couldn't load. You need to fix something in your PHP page.
First, I would not use the .get()
command and instead use the .ajax()
command. The .ajax()
command includes methods to abort on failure (.fail()
) and to stop trying after a set amount of time (timeout:
).
$.ajax({
url: url,
type: "GET",
timeout: 1000,
data: { 'mode':9,
'startDate':$('input[name=startDate]').val(),
'endDate':$('input[name=endDate]').val(),
'christened':$('input[name=christened]:checked').val()
}
}).done(function(data) {
alert('success!');
// ...
}).fail(function(jqXHR, textStatus, errorThrown) {
alert('fail :(');
// ...
});
Second, your SQL can be cleaned up. As @Shivan pointed out, many of the words in your query (e.g.: date
, time
, record
) are Keywords: words that are used as functions or datatypes. MySQL gets confused if you try to use one of these words as a column name, so you need to escape it with the backtick (`
) character.
I've also put the dates inside of single quotes ('
)
SELECT
`name`, `barcode` , COUNT( * ) AS `times`, `christened`, `gender`
FROM (
SELECT
`name`, attendence.barcode as `barcode`, `DATE`, `TIME`, `christened`, `gender`
FROM
`attendence`, `record`
WHERE
`attendence`.`barcode` = `record`.`barcode`
AND `DATE`
BETWEEN '$startDate'
AND '$endDate'
$christenedClause
GROUP BY `name`, `DATE`
)A
GROUP BY name
I don't think that this will fix all of your problems, so you should enable error reporting on your PHP page. Put this code at the top of duty.php
:
ini_set('display_errors',1);
error_reporting(E_ALL);
If this works right, then PHP will display error messages instead of just failing.
Add whatever something in the $data array so that it won't return an empty json. At the end of duty.php add
...
if( count($data)==0){
$data['content']=['none'];
}
echo json_encode($data);