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.
Related
I have a flash banner working with xml and I want a control page for it. For example, when I change banner width from control page also banner change at the same time and same page without refresh. So that occur a preview for flash banner. PHP,HTML and banner available. But how can I get data from form instantly
if "instantly" means without refreshing the page, you need to search for AJAX.
here's a starting point for you:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
or, directly from flash:
Flash - AJAX call
You need to re-write the code with AJAX, modify all size-related properties for the swf. The script will monitor the form, and onChange (not onSubmit), read the values, remove the previous code and write a new one. I don't think that simply changing the values of the previous object will work on the fly.
i was wondering how can i reload a .php website containing a table every second with ajax? is it fine if i use javascript, but a quick php script would do just fine, does a loop for reload method work?
I tried ajax with javascript but every time i used jquery load method it didnt load anything, and i
see all ajax examples only deal with php websites that accept queries with get.
I just need my webpage to reload and check for changes in the database as quick as possible.
i tried the following code but nothing:
$("#newnav").load("polling.php");
<meta http-equiv="refresh" content="5; URL=http://www.yourdomain.com/yoursite.php">
Pop that in your HTML code inside the section. That will automagically reload the page every five seconds. Change the value to suit whatever time in seconds you want.
I want to put Thumbs up/Thumbs down buttons on my website.
There will be quite a few of them displayed at once, so I don't want to have to do a POST and reload the page every time the user clicks on one.
I thought of using re-skinned radio buttons to choose Thumbs up/Thumbs down, but that would require the user to click a submit button.
So how do I do this? I am open to using JavaScript or some other form of Client-Side scripting, so long as it is built in to most/all web browsers.
Thanks!
YM
I would take a look at using jQuery, http://jquery.com/ It is a WIDELY used library and there is tons of support for it both here and # jQuery's website.
You could easily assign all those thumbs to do an ajax post to a save page with the correct id and the user would not know the difference
You're definitely going to need to use JavaScript on this. Well, there are other client-side languages that could technically do the job (ActionScript, for example), but JavaScript is definitely the best way to go.
Look into AJAX (Asynchronous JavaScript And XML). This is just a buzzwordy way of saying use the XMLHttpRequest() object to make page requests with JavaScript without reloading the page. Here's a good tutorial: http://www.w3schools.com/ajax/default.asp . Note that, despite the word "XML" being in the title, you don't have to use XML at all, and in many cases you won't.
What you'll basically do is this:
Have your thumbs-up and thumbs-down buttons linked to a JavaScript function (passing in whether it's a like or dislike via a function argument).
In that function, send a request to another page you create (a PHP script) which records the like/dislike. Optionally, you can have the PHP script echo out the new vote totals.
(optional) If you decided to have your PHP script output the new results, you can read that into JavaScript. You'll get the exact text of the PHP script's page output, so plan ahead according to that -- you can have the PHP script output the new vote totals in a user-friendly way and then just have your JavaScript replace a particular div with that output, for example.
Why use AJAX for dynamic web pages when you can do it only with php?
The main reason to bother with AJAX is User Experience (UX).
Now AJAX won't necessarily improve UX in every single instance so in a lot of places sticking with pure PHP is perfectly okay.
But imagine the case where you have a text field on the site and a link to vote on something. Kinda like this site. When you add AJAX your users won't loose the text they entered in the textfield when they decide to vote on the link! How incredibly useful!
So if you care about your user's experience it is a good idea to use AJAX in situations like that.
PHP creates and outputs the Content to the Client Browser as it's a Server-Side Language and that's what it was built for, so on a request your code will access database, files etc. and then output the constructed html/text to the client.
Ajax just gives the User a more Desktop like feel. For example deleting a record and instead of the entire page reloading just letting the one element disappear from say a list and letting the server know that the record is to be deleted. But Remember to let the User know when you are busy sending data to the server (With a progress bar in .gif format for example). As lot's of user feel that if nothing happens on the screen to notify them, that the application is frozen which means they will either reload the page or just try to click the button again.
But you will need to provide some sort of compatibility with browsers that have Javascript disable and thus cannot use your AJAX functions, just something to keep in mind.
AJAX stands for Asynchronus Javascript and XML, meaning that a page can get new data, without having to reload a page.
PHP cannot send data without reloading the whole page. A user has to press a button, to send data.
An example of AJAX is for example google suggestions or the tag suggestions on this website.
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.