Php polling Is it required - php

I want to write a php code that displays a html button only till some point of time.
Since php is a scripting language and code get executed only when the html page is rendered,
I was thinking that instead of polling I can just check the time at the time the page is getting rendered. If the current time does not satisfy my condition then I simply won't echo that button else I can echo the button.
Is this approach right or should I do polling ?

Once the page loads, php has no more control. If you have displayed a button, and you want to remove it after a certain time frame, you will need to use javascript anddo some kind of setTimeout() to schedule that. Obviously, the javascript won't be 100% reliable (if someone has js disabled, or a browser not supporting js).

I don't understand why you would use polling. PHP already has all needed functionality to both check the time and to conditionally output HTML. The only thing that may cause trouble is timezones, but that can be handled by adding an offset to the relevant times.

Related

Checking for variable changes based on time intervals (PHP+HTML)

so i am just wondering if i could do this:
a basic site, nothing on it but 4 different photos that have different priority levels which indicate the time-span of actually displaying an image ( lvl1=1min, lvl2=3min, lvl3=10min...and so on)
How could i do this with html and php.. I am not sure if answer is really basic but i cant seem to get my head around it.
Is html code running parallel to php code or does an infinite while loop in php stop the whole html code in process?
I was thinking of creating an infinite while loop in but i am worried it would eventualy somehow crash the site?
I guess i dont understand how html code runs..
Thanks to anyone who helps.
As OP mentioned. You have to use JavaScript. PHP is for server side processing, once it send final output to web browser you have to request again from the web server. You can use AJAX for your purpose easily.
In the context of the WWW (and simplifying a bit):
PHP runs
The output of PHP is received by the web server
The web server sends the output to the browser
The browser displays the page
You can look at the current time and use that to decide what <img> tag to send to the browser.
You can't change an image already displaying in the browser using server side code. The code has already run. New code won't run unless the browser makes a new HTTP request.
If you want to change the image displaying in the browser you need client side code. For all practical purposes that means JavaScript.
You can use the timer functions to run code after time has passed and DOM to change the HTML elements already on the page.
Understanding bits from your question this may be what you need
PHP code will send all four images to the browser in img tags. You can add priotity to one of the attributes to each tag for example <img data-priority="1" src...
When this code is fully loaded in browser you start a timer in javascript which then looks at current time and subtract it from time it loaded and get number of miliseconds. You can get all this using date and time functions in javascript.
At each time difference you check if that requires an image to be hidden or shown and then show or hide it. Look at jquery show and hide functions for html nodes.
If you want to show one image just hide them all in beginning and then show the one you want according to whatever rule you have either server side or client side. PHP will help you narrow down rules on server and send only required data to client however you can use simple html to hide and show if its not critical and you will avoid making multiple calls to server for images.
If this is the scenario you are looking for then try it in jsfiddle or code with sample images. You can get 4 sample images from http://lorempixel.com/ and use javascript to play with it.
For you purpose you can use AJAX which is update your code on given time interval.
If you want to set something on a timer, you can use JavaScript's setTimeout or setInterval methods:
setTimeout ( expression, timeout );
setInterval ( expression, interval );
Here expression is a normal function and timeout and interval are integers in milliseconds. setTimeout runs the timer once and runs the expression once whereas setInterval will run the expression every time the interval passes.
So in your case it would work something like this:
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
As far as the Ajax goes, see jQuery's ajax() method. If you run an interval, there is nothing stopping you from calling the same ajax() from other places in your code.
Anymore expression need please let me know.

PHP website basics, some clarification please

I currently am using PHP to render a dynamic JS+CSS+HTML website via echo statements. The PHP is filling in some of the JS variables.
I want to expand this to include If/Else statements but I have some questions about how PHP interacts with the rendered page
After the site is rendered (all the echo statements have printed) can I still call additional PHP functions? For instance, if I click something can I have that call a PHP function?
Feel free to point me toward PHP tutorials that explain how PHP interacts with the site. Ironically I have done a lot of PHP coding for other tasks I just never thought about it from the ground up
PHP does not interact with the site. PHP's only function is to output text to the browser.
Look at it this way: if you had no PHP, the website would be composed of HTML pages. HTML pages can contain JavaScript and the user can interact with them. The difference with PHP though is that you can generate these HTML pages dynamically (which by association means that you can also generate the JavaScript contained in the HTML pages dynamically). Nothing else has changed.
Basically once the PHP script has finished running and printed (echoed) all the html content to the browswer then it stops running.
Essentially if you are clicking on a web page then that can either call a new page via a form POST or a GET OR maybe javascript can handle the click. And then that Javascript can perhaps perhaps trigger a new request. And that new request can call a PHP script which performs whatever task you want it to.
So if you click on an <a href='action1.php?param1=yes'>Click here</a> link then that will call the script action1.php (with $_REQUEST['param1'] set to 'yes') on the webserver and the webbrowser displays anything it returns. And that new html replaces all the html currently in your browser window.
OR if you have a form:
<form action='action2.php' method='POST'>
<input type='text' name='stuff'>
<input type='submit>
</form>
And you click on submit then again the webserver will be called but this time the script action2.php will be called with $_POST['stuff'] set to whatever is in the text field. And whatever the script returns will be what is displayed in the browser.
Now if you want to click on something in the browser and just change something small on the page or just perform some action on the server then you should probably investigate AJAX handlers and jQuery in particular
Good Luck.
I've learned in coding to never say never....
Aside from standard Ajax, there --IS-- a way to run PHP functions asychronously: xajax In a nutshell, it allows you to call PHP functions directly without reloading the page. A word of warning-- the documentation is weak....
PHP is server side technology as others have mentioned. Getting it to run asynchronously (or after the main page has rended, as the case may be) is only facilitated with some Javascript voodoo, which is what xajax brings to the table. It does a "good enough" job making this possible. I personally have chosen to use Jquery because of the sheer power it brings for UI development. There's tons of options out there to make Ajax calls.
The key to moving your understanding forward is thinking about coding for the FRONT END. When I was beginning my career I was solely a back-end guy, focusing on the standard if/thens/elses and echoing out the relevant code. Once it was on the screen, I was done with it. Moving over to concentrate on the front end requires understanding of the dom structure -- what you'll be using to "grab" elements and then understanding of tech such as javascript to "do stuff" with your data. In a nutshell, it's just a different way of doing what you're used to with a slightly different syntax. Rather than just jumping in feet first to tackle Ajax or this specific task, take some time to familiarize yourself with the basics of traversing the Dom, and perhaps some Javascript or Jquery basics as well. It'll make your job much easier in the long run.

How to change language without reloading page with PHP / Jquery

I am working on a multilangual website right now.
I am currently including the related language file ('lange/_en.php') for language phrases.
To change languages users will select their language from a < select > item. The thing i want to do is changing related phrases (and urls too if possible) in the page without refreshing or submitting the page.
I remember i saw something like this in web but i have no idea where.
Any help or any ideas about how this thing can be done?
The issue with this is that a language change doesn't only affect a small section of the page, it affects the whole page. So really, you are left with three choices.
The simple way which is indeed reloading the whole page. It's easy to implement, easy to maintain, and doesn't require you to make sure that JavaScript currently running on your page is aware of the new language at runtime.
The complicated way which is getting all the new markup via AJAX and replacing the content of the <body> tag with the reloaded content. This will cause issues with other scripts running (such as image carousels, etc.) that holds a reference to an element so you have to reinitialize every single script that is running on settimeout() on your page.
The close to impossible way which is to have a client side dictionary, selecting each relevant tag, and changing its contents with the new language. This is a pain to setup and a pain to maintain. You literally need a section tailored to each specific page. Again, if you have scripts with strings, you'll have to make sure that the strings they use are updated to the new language.
You are better off simply reloading the page. It will work without JavaScript and it's a one time deal that won't bother users.
check this plugin out:
http://keith-wood.name/localisation.html
It changes language 'on the fly' without going back to server side.
Image reload prototype if-modified i posted about image content update to selected language without reloading the page.
The image takes the value thats sent and updates to language/country code (which is bound to language) on DB and updates image content (characters) to match their keyboard layout..
Hope its some use

Page changed in JQUERY

I am trying to found out how to see if a php file has changed and then show a div with saying Page changed in JQUERY
You'd better do that in PHP using filemtime, no need for JQuery here.
You only need jQuery for this task if you're trying to detect the page change without waiting for the user to request a new page. If not, do as the other responder suggests and use PHP.
But if you need to do it without a page reload, use one of the $.ajax() methods in jQuery in combination with a JavaScript timer. You'll have to poll the server periodically (thus the timer) to ask if the page has been altered.
You would also need to set up something on the server that can tell your page about changes. Perhaps a very simple service that provides the timestamp of the last edit in JSON format. Use $.ajax() to poll for the timestamp, then compare it with the last edit the page knows about. If the timestamp from JSON is more recent, display your div.
Javascript cannot access the server, you will have to use some sort of server side technology. Like PHP that was suggested by Pekka.
In short, javascript is client side, which means it interacts with the user on their side, while php is server side, meaning it interacts with the server. Checking the file modified date is a server side issue, your client isn't serving the pages (unless you're on freenet)
Or you could output a <meta> tag for when the page was updated with PHP or whatever framework or language you are using. Then create a cookie with your JS and compare the cookie with the meta tags content.
Ugly solution but it would work. I wouldn't want to resort that this however.

Refresh a PHP page for every predefined seconds

I would like to load/refresh a particular page for every 10 secs to view updated data's fetched from Database.
I used META for doing it
<META HTTP-EQUIV=Refresh CONTENT='10; URL=livedata.php'>
But still i agree we also do this by
using :
Javascript to load a div by settimeout
Ajax dynamic refresh
Would be great if you share the performance issues using META , AJAX dynamic refreshing , Javascript settimeout .. Also share the best way of doing it.
Note : Need to refresh whole page rather than specific frame or div.
Using AJAX is the least intruisive to the user, because the user doesn't notice that something is being refreshed/reloaded until it is complete.
Please note that AJAX can perform better or worse than META depending on the situation:
If the data to be updated is small with respect to the full HTML page size, AJAX is better than META, because with AJAX you can send only the data difference, and/or you can send data in more compact format than HTML.
Running JavaScript puts a burden to the user's browser. If the user has 20 tabs open (which is not uncommon), and each of them runs some setTimeout in the background, it can make a huge difference in browser respoinsiveness to convert all of them to JavaScript-free refresh.
If you plan on refreshing the entire page, using <META> tags is the cleanest way. It just seems awkward to have a JS timer refreshing your page when you have a fully-supported HTML-only way of doing this.
However, if you just need a specific part of the page refreshed, use AJAX. It's better in terms of user experience, as well as performance.
using javascript to fetch dynamic content has one more benefit: if the content doesn't load for one time, it can still keep trying. if you reload the whole page and it doesn't load, then it would stop there.
also if you use Ajax, then the display is nicer because you don't see the whole page blanking out and re-rendering again and again.
For the client, there's really no difference between all the methods you mentioned. The only difference I can find is that using doesn't require javascript, like other solutions do, but nowadays everybody has javascript anyway.
The big difference, to me, is in the server usage. If you have 100 users refreshing every 10 seconds, that's already about 10 reqs/sec. Depending on the logic you have to generate the page (which is likely dinamic), this may cause the server usage to skyrocket. Make sure you're careful about that.
note that you can also use header() in PHP to accomplish what the meta tag is doing too. Just make sure you make the header() call before other output.
With jQuery you can do it):
var seconds = 10;
var id = setInterval(function()
{
$('#container').load('whatever.php');
}, 1000*seconds);
:)

Categories