I am working on a timetable for a school shedule.
On the site, there is a jquery ui datepicker, that can be clicked to update the timetable (based on the date that has been clicked on the datepicker)
Everything works except I have to click twice to update the timetable. So every other click gets the job done.
I narrowed my problem down to several points:
Caching - The browser uses the cached Data for the time table
Caching on the PHP side - I have maybe not set the correct headers to tell the browser not to cache data - Tried several headers - Maybe I am doing it wrong
I have to set the Ajax option caching to false - Tried it- Not Working
I have to maybe make the call syncronous so the browser waits for the response - Not sure about this - tried it though
I am making an ajax call inside the jquery ui datepicker onselect option. Like this:
Jquery Ajax Code
onSelect: function (date) {
//defined your own method here
// $("#timTableMon").empty();
// $("#timTableTue").empty();
// $("#timTableWen").empty();
// $("#timTableThur").empty();
// $("#timTableFr").empty();
$.ajax({
url : 'ajaxDate.php',
dataType: 'json',
cache: false,
type : 'post',
data : {
'sendDate' : date
},
success : function(data, status) {
$("#weekHeader").text(data.week);
$("#timTableMon").html(data.Mon);
$("#timTableTue").html(data.Tue);
$("#timTableWen").html(data.Wen);
$("#timTableThur").html(data.Thur);
$("#timTableFr").html(data.Fr);
// location.reload();
// window.location.href = "http://localhost /timeTable /public/test.php";
},
error : function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
}); // end ajax call
PHP Code
$date = $_POST['sendDate'];
// $log->log_action("date from ajax", $date);
$date = new DateTime($date);
$week = $date->format("W");
// $log->log_action("week from ajax", $week);
// $log->log_action("week from ajax", $week);
$_SESSION['week'] = $week;
$timetable->week = $week;
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.
$messages = array();
$messages['week'] = $week;
$messages['Mon'] = $timetable->drawMon();
$messages['Tue'] = $timetable->drawTue();
$messages['Wen'] = $timetable->drawWen();
$messages['Thur'] = $timetable->drawThur();
$messages['Fr'] = $timetable->drawFr();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
echo json_encode($messages);
Any help would be greatly appreciated. Thank you
Related
I'm trying to set up a PHP server send event, which works okay. But at random intervals it is pushing the same data repeatedly.
Here's a quick scenario to clarify what I'm describing: Let's say I insert a db record at 1:00:00. The record's data is pushed as it should. However, at 1:03:00 that record's data is pushed a second time. Then at 1:03:17, it is pushed again. And I now have 3 instances of the record displayed.
Why is this happening, and why at random intervals?
I increased php execution time, but the issue is still occurring.
In the browser console, I'm getting this error: net::ERR_INCOMPLETE_CHUNKED_ENCODING.
I have this for client side:
<script>
var source = new EventSource('pdo_updates.php');
var pdo_updates;
source.onmessage = function(e) {
pdo_updates = e.lastEventId + '' + e.data + '<br>';
document.getElementById("videoID").innerHTML += pdo_updates;
};
evtSource.close();
</script>
And this for server side:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0');
function send_msg($id, $msg) {
echo "data: $msg" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
$last_event_id = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : False);
if ($last_event_id == 0) {
$last_event_id = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : False);
}
$last_id = 0;
try {
$conn = new PDO('mysql:host=localhost;dbname=my_db', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
while(1) {
$id = $last_event_id != False ? $last_event_id : $last_id;
$stmt = $conn->prepare("SELECT id, message FROM messages WHERE id > :id ORDER BY id DESC LIMIT 1");
$result = $stmt->execute(array('id' => $id));
$stmt->bindValue('id', $id);
if ($result) {
while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($data) {
send_msg($data['id'], $data['message']);
$last_id = $data['id'];
}
}
}
sleep(1);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
So, after trying just about every keepalive related header, and every apache timeout adjustment config, I ended up running a packet capture. I discovered there was a TCP reset was being triggered by the remote end. I have my site behind Cloudflare, and once I disabled Cloudflare the issue partially disappeared. The TCP session was being refreshed every 100 seconds and would cause the last message to appear again when that happened. This was responsible for the browser console error: net::ERR_INCOMPLETE_CHUNKED_ENCODING
At the same time, however, there was an issue in my submit code. But I'm not 100% sure why.
$.ajax({
type: "POST",
url: url,
data: $("#submitmessage").serialize(), // serializes the form's elements.
success: function(data)
{
$("#myvideo").val("");
}
});
I needed to clear the value in my input field after submit. I haven't had any duplicates since.
I am having trouble executing this code in my index.php.
It says 'CartAction not set'
I need your help php gurus. I can display any files you need to fix this error.
Here is the code:
// Handle AJAX requests
if (isset ($_GET['AjaxRequest']))
{
// Headers are sent to prevent browsers from caching
header('Expires: Fri, 25 Dec 1980 00:00:00 GMT'); // Time in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: text/html');
if (isset ($_GET['CartAction']))
{
$cart_action = $_GET['CartAction'];
if ($cart_action == ADD_PRODUCT)
{
require_once 'C:/vhosts/phpcs5/presentation/' . 'cart_details.php';
$cart_details = new CartDetails();
$cart_details->init();
$application->display('cart_summary.tpl');
}
else
{
$application->display('cart_details.tpl');
}
}
else
trigger_error('CartAction not set', E_USER_ERROR);
}
else
{
// Display the page
$application->display('store_front.tpl');
}
It's because your code is expecting a parameter named 'CartAction' in the url
Example:
www.yoursite.com/?CartAction=ADD_PRODUCT
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character. Source
You check if $_GET['CartAction'] has a value ( from the above url this superglobal variable has the value 'ADD_PRODUCT' )
What #Mackiee (in comments) and your error message are both telling you is that the problem is that there is a query parameter missing. The URL that calls this needs to include either ?CartAction=ADD_PRODUCT or &CartAction=ADD_PRODUCT
I want to cache the data in broswer so that broswer don't need to query the server in several minutes. I added php cache header but seems it doesn't work. Here is my ajax code and php code:
Ajax code:
function myAjax(name, callback) {
var url = './php/getJson.php?name=' + encodeURIComponent(name) + '&callback=?';
jQuery.getJSON(url, function(data) {
callback(data);
jQuery.ajaxSetup({ cache: true });
});
}
PHP code:
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
$lm = gmdate("D, d M Y H:i:s", time() - $seconds_to_cache/2) . " GMT";
header("Last-Modified: $lm");
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: public, max-age=$seconds_to_cache");
include_once('getData.php');
$output = $_GET['name'];
echo $_GET['callback'].'('.$output.')';
Thanks for MitchS and lenzai's help. This issue is solved. The cache:true option should be set before any ajax querying and old jquery libraries don't support caching. So make sure you are using the newest jquery library
For people who want a working example:
Ajax code:
var callback = function(data) {
alert("callback");
}
function myAjax(name) {
var url = './php/getJson.php?name=' + encodeURIComponent(name) + '&callback=?';
jQuery.ajaxSetup({ cache: true });
jQuery.getJSON(url, function(data) {
callback(data);
});
}
PHP code:
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
$lm = gmdate("D, d M Y H:i:s", time() - $seconds_to_cache/2) . " GMT";
header("Last-Modified: $lm");
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: public, max-age=$seconds_to_cache");
$output = '{"eventList":["event1","test event"]}';
echo $_GET['callback'].'('.$output.')';
You are setting the Last-Modified header to an hour ago and setting the max-age to be an hour.
This means at the point you send this data, it is already at the maximum age allowed for a cache and any subsequent requests must be resent.
I wrote a test project adapted from your on your code.
http://test-lenzai.rhcloud.com/index.html
Each time you press the button, it launches a new ajax request...
The query is sent twice then the browser cache is used!!!!
This is caused by JQuery :
the first query is appended &_=s135227990... ( some timestamp probably)
subsequent queries don't have this extra argument and all identical queries are effectively using cache.
Now the question is how to tweak jquery so that 1st and second ajax queries are identical
I am trying to call a PHP function using AJAX. Below is the script I used.
<script type="text/javascript" src="jquery.1.4.2.js">
$(document).ready(function () {
// after EDIT according to
// #thecodeparadox answer
$('#local').click(function(e){
e.preventDefault();
e.stopPropagation();
promptdownload();
});
});
function promptdownload(e)
{
$.ajax({
type: "POST",
url: "js/prompt.php",
data: { "get" : "runfunction", "action" : "promptlocal" },
success: function (response) {
}
});
}
</script>
The corresponding PHP code (prompt.php) is:
<?php
$path1 = "downloads/1.jpg";
$browserFilename1 = "Local Travel";
$mimeType1 = "image/jpeg";
function promptToDownload($path, $browserFilename, $mimeType)
{
if (!file_exists($path) || !is_readable($path)) {
return null;
}
header("Content-Type: " . $mimeType);
header("Content-Disposition: attachment; filename=\"$browserFilename\"");
header('Expires: ' . gmdate('D, d M Y H:i:s', gmmktime() - 3600) . ' GMT');
header("Content-Length: " . filesize($path));
// If you wish you can add some code here to track or log the download
// Special headers for IE 6
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
$fp = fopen($path, "r");
fpassthru($fp);
}
if ($_POST["action"] = 'promptlocal')
{
promptToDownload($_GET[$path1], $browserFilename1, $mimeType1);//comments
}
?>
This is how I code the button that is supposed to trigger the function:
<input type="button" id="local" name="local" value="Local Travel">
My expected output is to have this button promt the user: "where to save 1.jpg file".
However I couldn't make it work.
Any advise is highly appreciated.
$('local').click(function(e){
should be
$('#local').click(function(e){
As local is an id so you should use # before it. And also in your php code there are some missing quotes.
Use Firebug(FF), Dragonfly(Opera), Developer Tools(Chrome). You can see all javascript errors, warnings and exceptions, and can see ajax requests data.
data: { "get" : "runfunction", "action" : "promptlocal" },
Try to remove the quotes from "get" and "action".
Like this :
data: { get : "runfunction", action : "promptlocal" },
It looks to me like you are trying to download a file with jquery/ajax. You will not get this to work with only ajax. This question has been answered several times on stackoverflow.
I hope this link will help you: Ajax File Download using Jquery, PHP
I have a simple search form with a search box and a result box.
When I type a search word a request is created like: http://www.site.com/php_handler.php?s=hello
In the php script and a result is given back to the script this way:
<?php return $s; ?>
The problem is that my htmlrequest stops at readyState 3 it doesn't get to 4.
The javascript looks like this:
var xmlhttp = sajax_init_object();
function sajax_init_object() {
var A;
try {
A=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
A=new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
A=null;
}
}
if(!A && typeof XMLHttpRequest != "undefined")
A = new XMLHttpRequest();
if (!A)
sajax_debug("Could not create connection object.");
return A;
}
function getSearchItem()
{
gs=document.forms.mainform.resultsfield;
var searchword=document.forms.mainform.searchform.value;
if (searchword.length>=3)
{
setWaitCursor();
clearResults();
var uri = "http://site.com/ajax_handler.php?s="+searchword;
console.log(uri);
xmlhttp.open("GET", uri, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4) {
processResults(xmlhttp.responseText);
removeWaitCursor();
}else{
console.log(xmlhttp.readyState);
}
}
xmlhttp.send(null);
}
else
{
alert("please add at least 3 characters .");
}
}
Can someone tell me why it stops at 3?
edit: here is also the php code:
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
session_start();
//include main file
require_once($_SESSION["FILEROOT"] . "xsite/init.php");
//check if formulier is posted
$zoekterm = C_GPC::getGETVar("s");
$s="";
if ($zoekterm != "") {
$query="SELECT number,name,city,zib,zip_annex FROM articles WHERE version='edit' AND (naam LIKE '%$school%' OR brinnummer='$school') ORDER BY name";
if ($rs=C_DB::fetchRecordSet($query)) {
while ($row=C_DB::fetchRow($rs)) {
if ($row["plaats"]!="") {
$s.=$row["name"].", ".$row["city"]."|".$row["number"]."\n";
} else {
$s.=$row["name"].", ".$row["zip"].$row["zip_annex"]."|".$row["number"]."\n";
}
}
}
}
return $s;
?>
edit:
I missed a semicolon in my php script and now the ready state only gets to 2
edit:
The problem is even different. It gets to 4 but it doesn't show the result text.
1> Don't send Cache-Control: post-check=0, pre-check=0. These don't do what you think they do, and they're entirely unnecessary.
2> Your AJAX results page needs to send a Content-Length or Connection: Close header.
3> Try adding a random to your request URL to ensure you're not looking at a stale cache entry.
ReadyState 3 => Some data has been received
ReadyState 4 => All the data has been received
Maybe the XMLHTTPRequest object is still waiting for some data.
Are you sure your php script ends correctly ?
Is the content-length alright ?
To debug this you have two options, type the URL directly into the browser [since you are using a GET] and see what is happening.
OR
You can use a tool such as Fiddler and see what is exactly happening with the XMLHttpRequest