Javascript not reflecting changes to a file - php

So, I'm a bit confused here.
I'm slowly progressing into ajax or HTML5 SSE, because I have list that, in the long run, I want to update without the user having to refresh. But, baby steps here, I'm starting with a simple function that should tell me when the file the list is reading from here, has changed, from another computer plugging in an entry.
CODE:
<script type='text/javascript'>
function CheckForChange(){
alert("<?echo (count($listArray)) . ' and ' . count(file($filename_noformat))?>");
}
setInterval("CheckForChange()", 7000);
</script>
listArray is the PHP variable that keeps the list seen on the page when the page refreshes, read line by line from a text file (I'm working on moving to a database, later)
Since PHP variables can only execute when the page is loaded, the PHP would only reflect what was in the text file when the PHP executed.
But with Javascript, and the setInterval function, shouldn't it be able to execute the PHP to check what's in the text file at the time that function is executed (every 7 seconds)? Because it's not, and I don't understand why not.
I try plugging in a 4th post to the list on my phone, and the phone's alert changes to 4 and 4, but the computer still says 3 and 3. I want it to say 3 and 4.
Thanks

setInterval() executes local javascript in your page, not server-based PHP. If you want it to send something to your server without reloading the page, you would have to use an ajax call to actually send something to the server which could cause some PHP to execute.
Hint - look at View/Source in your browser and see what is actually in your page. There's no PHP in your page. The PHP runs on the server once when the page is created.

No.
The PHP runs on the server and outputs some text.
The server sends that text to the client (the browser).
The browser then interprets that text as HTML and JavaScript.
The PHP runs only once. The JavaScript function it generates then is run repeatedly, but from JavaScript's perspective, that function has a string literal in it, not executable server side code.
If you want to run PHP then you have to issue a new HTTP request to the server. This requires either reloading the page or using Ajax.

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.

Jquery Post/AJAX - Does closing window with code affect server script?

To preface, I know this isn't a great question and it will be hard to explain.
I have a PHP script that takes 5-10 minutes to run. I don't want the user to have to wait for it. If I "trigger" the script using jquery ajax, and then the user navigates away from that page or closes the browser (and doesn't wait for the response (if any) which will come much later), will the script still execute fully (assuming there are no errors etc)
Thanks!
Once the server receives the AJAX Request along with the data, it would process it as usual, even if you close the page or the window. If you close the browser window before the server receives the AJAX Request, the processing it not going to happen.
Furthermore, if the AJAX Request is returning any kind of data or displaying messages, it is advised that you leave the window open, so that there is some "Listening" page to the Server's Request Response.
In your PHP script you could call the ignore_user_abort(true) function which would cause the script to run regardless of the user closing the page or not.
You could use the command line if you have access to one:
php /loc/to/file.php
This does not have a timeout and might be faster than port:80 (a browser calling an phpfile)
Or call the main phpfile in another file via a php's exec():
<?php
exec("php /loc/to/file.php > /loc/to/result.txt");
?>
You might want to use shell_exec()
The dir of results.txt and the file itself need to be writable.
The 'greater than' sign writes the output of the phpfile to result.txt. If the php would echo 123, that would be the contents of result.txt
5 to 10 minutes is a very long time, you might want to check your code for improvements. If you are using a database, add indice (an index) on columns u use allot, that ccan save huge amounts of tim

when a browser parses a PHP file (containing JavaScript), which language parses first, PHP or JavaScript?

I'm confused about this question. Actually I'm trying to get a JavaScript value to PHP variable using Ajax, but I'm getting error. I think that PHP is parssing first before I could put the value to it via JavaScript
PHP is serverside means it runs on the server, when you get your page php already run through is finished and made his output and then JS starts to work on your computer (clientside). ;)
The PHP is interpreted first on the server and the result is rendered to HTML and JavaScript as appropriate. When the client browser receives this result it will run any embedded scripts, including JavaScript. You also mentioned AJAX, which is a specific usage of JavaScript that makes a connection back to the server from the browser. An AJAX call is sometimes used to bring the value of a server side (possibly PHP) variable into JavaScript after the initial page contents have been sent to the browser.
PHP is parsed on server side, JS is parsed in browser. So, when client asks for a page, at first server runs PHP script to the end, generates HTML and puts it into browser, where JS starts runing.

How Ajax enables a Javascript string to be passed to php

I've been learning Ajax and now I'm wondering how it allows a string from Javascript to be passed to php.
It was said before that the problem with passing Javascript to PHP is that the PHP code gets run first, and then the Javascript gets run. So when Javascript generates a string it's already too late.
Does this mean that Ajax allows PHP code to be run after Javascript?
I think this is what they're getting at:
Before Ajax -- specifically, before XMLHttpRequest came along -- a single web page was served as a single page load. If it was a PHP-generated page incorporating Javascript, the browser would request the page, PHP would generate the page (including Javascript code, includes, fragments of script on the page, etc.), would send the page to the browser, and the browser would display it. So, the PHP happened up-front. Until the next page load -- when the entire page was refreshed from scratch -- PHP wasn't involved again.
After the advent of XMLHttpRequest, which helped put the "X" in "AJAX", as it was back then, you had another option. Once the page was loaded, your Javascript could make requests "behind the scenes" of the page, to request more information from the server, without reloading the page. In effect, the loaded page could cause more PHP to be run on the server, and display the results.
So, if you're considering a single page load from a PHP-based website, that is (sort of) what Ajax means; without it, you get a single PHP page-build that's delivered and then your Javascript has to run on that result alone. With Ajax, you can make further requests to your server and throw the results out onto the existing page without a full page load.
The php interpreter sitting on the server is basically interprets whatever php script into (usually) HTML page.
That's why you can never "pass" javascript variable into php as to the interpreter, your javascript is just yet another string, without any special meaning. Your javascript is run by your browser and doesn't even aware that it was being produced by PHP.
I believe that's what it means by "too late".
You should know, that Javascript is always (except Node.js) Client-Side.
PHP is a Server-Side language.
You can't pass Javascript variables to PHP - at least not at the Pageload.
What you can do is doing a AJAX-Request after the Page is laoded to send something to PHP.
With the Response of that call you can replace some other things on the current requested Page.
No it does not mean that "Ajax allows PHP code to be run after Javascript". AJAX is a new request to the server and it allows you to manipulate with the server's response. You can observe this by coping the url to which AJAX request is being made and pasting it into the browser.
So basically when you open a web site with the browser, you send a request and any AJAX call is also a request, but done in a background, so you cannot see it directly. You can use for example firebug or other developer's tool to see what happens behind the scenes. It has nothing to do with the scripts executions orders.

PHP $_SESSION not available until the page is reloaded. Why?

I'm creating a graph that does some database queries then builds the graph using GD functions then also puts the info from the database into an array that is stored in $_SESSION['my_data']. This graph is displayed on the web page using img tags.
<img src="my_graph.php?time=$time">
<?
print_r($_SESSION['my_data']);
?>
The problem is when I print_r() the array from the session variable it doesn't print the current graphs data; it prints the data that was in the graph last time the page was loaded. It's acting like a cookie.
I have tried putting session_write_close() immediately after I store the array in the session because I thought $_SESSION might have been locked until my_graph.php finished loading the image. That did not work.
I also tried putting sleep(10) before I print the array and that did not work.
Why this would happen?
I suppose, when your web server executes PHP code, which draws our page, it already has $_SESSION array initialized. And this array is not updated in current script runtime. When browser finds image tag, it makes another request to web server, which executes image generation script, which updates $_SESSION array in another runtime.
You may:
either make all calculations in your web page generation code
or reload page after image generation script completes calculations and sets all necessary data in $_SESSION array
If you are setting $_SESSION['my_data'] in mygraph.php, you will never see your $_SESSIONchange until your browser requests mygraph.php. This will never occur until the output is flushed to the browser (which will be AFTER you've already print_r() the $_SESSION).
You may be able to try flush() and hope the browser requests the image before you are done executing, I've never tried that (not sure if it will work). Though, sometimes you have to pad output with whitespace until it is about 2k (if I'm not mistaken). I wouldn't recommend this, though.
Another solution would be to request the page you have your code in above in the src. So if your code above is in test.php you could put <img src="test.php?img=true&time=$time">. Then if you get $_GET['img'], display an image, otherwise execute some code. Does that make sense?
Sequence of events:
Browser requests the main web page.
PHP runs the main page and sends the contents to the browser.
Browser receives the page.
Browser looks at the page and sends requests to load the graphics, etc.
PHP runs graph.php and sends the graphic to the browser.
Browser inserts the graphic into the page.
The important thing to note is that the whole of point 2 occurs before any of point 5 happens. This means that anything that graph.php does with $_SESSION will not be visible to the code in the main page, leading to the effect you're seeing.
This is the nature of a web page: the graphic files are separate from the main PHP program.
You cannot get around this using a separate graphic file the way you're doing it.
There is one way I can think of achieving it, but it would be a complete rewrite (it's up to you to decide whether it's worth it!)
It is possible to create a graph using Javascript. If you generate the javascript code to do this in the main page, you could then set $_SESSION during the graph generation code, and it would be available later in the program.
If you do decide to do this, you should look into the gRaphael library to help you out.
Hope that helps.

Categories