I have an application where I use ajax to update the comments & Like count.
I am using Codeigniter page caching http://www.codeigniter.com/user_guide/general/caching.html
and set following code to recreate caching every 60 minutes
$this->output->cache(60);
Problem is when somebody enter a new comment, DB operation is happening (Because of Ajax call), But the Newly inserted comment disappears after the page refresh, because of the cached HTML page. How to handle caching and also dynamic content to change?
The main purpose of caching is to save server resources on page load (i.e. so the server doesn't need to fetch all the dynamic data every time its loaded).
It sounds like you are using the AJAX function to both submit the data but also modify the webpage on the client side simultaneously. When the user then refreshes the page, this will fetch the content back from the server, in this case the original cached content that was generated before the comment was made and is therefore operating exactly as it should.
If you have a true requirement for caching this page, you could consider deleting the cache, which would force it to be rebuilt on the next page load whenever a comment is made. Placing the following line of code in your controller (in the function that received the AJAX data) should do it:
$this->output->delete_cache('foo/bar');
I found the solution.
To delete a cache file you need to use the following function
$this->output->delete_cache('CONTROLLER/FUNCTION');
Note: No Slash before or after.
In my Case i was using custom routes in codeigniter. When user type example.com it is routed to example.com/CONTROLLER/FUNCTIONNAME and that too default landing page.
So i created a new function. Please refer here
Related
I have a webpage that is auto-refreshed every 240 seconds with the generic HTML Meta tag. During the refresh it pulls data from a database which takes the site about 15 to 20 seconds to build before it's shown to the user. During this time I wish to show a small DIV with a loading message telling the user that it's loading data.
The more complicated thing about this is that the user has a few menu options to filter out specific data from the database. When clicking such an option the page is reloaded again and takes 15 to 20 seconds to build.
Users that aren't familiar with this loading time might feel the need to click the same menu option over and over again within a few seconds hoping that the page will load faster. But instead it will most likely cause the database server to get overloaded with requests.
So, to tackle this I wish to use jQuery to show a loading message, then have it load the data from the database (with a PHP script) and finally dump the data on the page.
I've done something similar but that was limited to users clicking a link which caused a jQuery script to load the data while showing the waiting DIV (using CSS rules).
I can't figure out how to implement this solution for an auto-refresh.
Some help would be nice.
You can use the same solution with auto-refresh as well, with the mention that the initial page load doesn't container the data that requires the DB call, but instead shows a loading message and starts an AJAX call to a server side script that returns the data.
Your page load:
Request
Server query DB
DB Response
Page loads (with data)
Ideal page load:
Request
Page loads (without data) <- loading message here
AJAX call
Server query DB
DB Response
Page updates (with data)
I'd second megawac's comment. Don't use a meta refresh. Also, 15-20 seconds is a very long time for generating a database report that is going to be generated every 4 minutes; odds are that you're bogging down your server pretty badly. Very few queries should really take that long, especially queries that need to be run nearly continually. I would strongly recommend refactoring your queries or doing some caching to speed things up. If you post some code, I'm sure people would be happy to look at it.
I'm writing a module for Joomla. It's going to be displaying data that it gets through a SOAP request. The problem with the SOAP request is that it can take up to 5 seconds to retrieve the data. This Joomla module will be on a page with many other Joomla modules and other content. I'm concerned that if this one Joomla module doing the SOAP request takes up to 5 seconds that it will delay loading the rest of the page. We have all been to websites which delay loading because of one part of the page and I don't want that to happen.
I am wondering if the solution is to have the Joomla module use AJAX (which I have no experience with yet) to do the SOAP request (currently it's being done with PHP) and somehow allow the rest of the page to load while it might take up to 5 seconds for the SOAP request to return data so it can be displayed.
What is a good workable solution for this problem? Caching the data isn't really an option because, it's timely. Thanks!
I would go with Ajax for this one.
Use window.onload() to trigger your ajax to fetch the data. The only problem is that the whole page will load before you fetch your data, resulting in potential delays displaying final data.
The code will vary a lot depending on preference but the principle of ajax is:
Create a placeholder or with placeholder content (or nothing)
On an event (in this case page ready) request data.
When the data is ready, replace the placeholder content with your data.
This has an added advantage that you can use a 'refresh' button to trigger the change too, so users can request updated data without reloading the original page.
Ajax is well worth learning as it is a doddle either using jquery or simple js. W3schools.com have an excellent tutorial.
I have a text file storing one string. I anticipate that the text file will be changing frequently, so in order to keep my page up to date, I would like to use PHP (preferably) to fetch data from the text file every 20 seconds so I can explode it into an array and use the contents accordingly. The variables would also need to update every 20s.
So: on page load, the contents are fetched and displayed. But the contents of the text file may be changed thus making the page outdated while a user may already have it open.
I tried META Refresh, but the whole page refreshes in the middle of browsing and interrupts the user.
Sorry for the confusing description, it's hard to explain. :)
I've searched the web for ages and not found an answer to my question. Please remember I am using a text file and not MySQL, since I'm only storing one string.
Thanks in advance.
If you want to stay with PHP, I'm afraid a refreshing HTML Meta is the solution :
<meta http-equiv="refresh" content="10; url=http://example.com/" />
Refresh the page every X seconds, so that the file gets reloaded.
Another way could be the use of frames, however I cannot seriously recommand it to you.
However, you can load a content without reloading the whole page, using Ajax. It allows you to perform a HTTP request to the server (using a Javascript code) and place its result on the current page, using Javascript as well. You could create a PHP script "my_string_parsed.php", which reads the file, and then parses/prints its content. Then, you could call this script through an Ajax request to http://yoursite.com/my_string_parsed.php, and place its result in a specified HTML tag on your page.
W3Schools.com provides an Ajax tutorial here : http://www.w3schools.com/ajax/
A warning concerning Ajax though : an Ajax content loading must never replace the typical HTTP behavior your browser and the server have. If the string in your file is the only content on your page, then the best solution would be the refreshing meta. Ajax should only be used to refresh parts of a page. Never the whole thing.
Why not using a database instead of a file. You could also use jQuery to update your page smoothly.
ok folks,
I have created a PHP page that is querying a database, and through a whileloop, displays the contents of that database table with a REMOVE and PUSH button. The REMOVE button removes it from the database entirely, and the PUSH button pushes that entry into another database and sets a variable that the entry has been pushed.
What I'm running into is that I can't quite get the page to refresh, in turn running an new query of the first database and displaying only those entries that have not been removed or pushed.
I can only get the query to run correctly if I manually refresh the page, whether it be F5 or control+r (command+r).
What is the proper way to refresh the page so that the query will run again on page load?
If you want to reload the page using Javascript, try this:
window.location.reload(true);
You can also see this answer:
How to reload a page using JavaScript?
there are two ways
If putting extra load on db is not a problem, use jquery methods likes $.get()
$.get('url',{},function(data){
//load results in appropriate div;
});
If you don't want to put any extra load on database just hide the row when it is removed or pushed.
$('.remove').click(function{
$(this).css('display','none');
});
similarly make it for pushed
Do you have some extreme caching setup on your web hosting solution?
If maintaining nice-looking URLs on this page is a non-issue you could always set a timestamp in PHP and append it to the string.
I'm not big on PHP but a javascript example would look something like this.
ts = new Date();
urltorefresh += '?timestamp=' + ts.getTime();
location.href = urltorefresh;
This would make sure the page is absolutely not in the browser cache since this specific URL have never been requested before.
Let's say we have a page written in PHP. This page loads by it self a template with header, body and footer and print this out. Now let's say that in the body of this page we would like to start a loop and load some posts (messages taken from a database).
We also need the page to load new posts every 10 seconds, if any, without refreshing the page (classic AJAX). This ajax call will use JSON and AJAX and micro templates.
Now i'm just wondering:
Do we really need PHP to load posts the first time the page is loaded? Can't we just start that Ajax call and load posts with Ajax instead? (Notice that the existing ajax call would be kept as it is, since it loads posts starting from the latest loaded (in case of no posts, that would mean all posts).
If you did not understand my question don't hesitate to let me know.
In this situation I think the simpler approach is the let AJAX handle it, if you do let php load the initial messages, you'll have two places in code, that you'll need to maintain to perform identical jobs.
I think you are asking how you should load the posts the first time the page is accessed. If so: When the page firsts loads, have some PHP that prints out the existing posts. Then, add some JavaScript to update the page with new posts every 10 seconds. This is a matter of preference. You might want there to be no posts when the page first loads, and then use Ajax to get the existing posts once your page has loaded.
Edit:
I agree with jondavidjohn that you might be better off using pure Ajax. However, you could always isolate the code that fetches the pages into a separate function. That way, the script that generates the page calls the same function as the script that is called via Ajax.
The drawback with that technique is that it doesn't downgrade gracefully. So people with javascript disabled will not see any posts.
I'd recommend outputting some data with php - AJAX requires JavaScript which many people don't have activated.
Why not, instead of having the browser poll the server for new posts, have the browser push new content to the browser when it is available using the likes of node.js?
I designed my site with AJAX exclusively, and it works perfectly except for one rather major issue: Using AJAX requires JS to be enabled. Of course, if users trust your site, this is not a problem, but if they don't, then an AJAX solution won't work unless you put the entire page in a noscript tag.