How to create a Loading/Splash Screen in PHP? - php

How do I create a loading overlay(with loading gif) while php script runs and returns data

PHP is a server-side language and what you're looking for is something that interacts with the browser on the clientside.
You're probably best of using a solution involving AJAX - for example using Jquery:
When the user loads the page, make an AJAX call that runs your script. Show a div that displays your 'loading' gif. When the AJAX call finishes, hide the div with your 'loading' gif.

You need to provide more information as to what to show and when to show the image. but to start with here is post about hot you can show a "loading" message (gif) using php and JQuery.

Use AJAX-like technologies and 2 php-scripts. The first should output only "loading..." div and start of AJAX-request to the second one, which returns the full content of your page.
PS: actually, I'm not sure if you understand, that "loading" exists on client-side browser as php's output, but php script runs on server before your browser gets it's output... read more about how HTTP and web servers work - this knowlege is quite neccesary for any good web-developer.

Related

Get HTML code after javascript execution using CURL PHP

I want to get the html source of a webpage genereted by javascript using Curl(PHP)
I tried the curl but I get just a javascript code :(
Can I use ruby to resolve my problem ?!
The javascript is executed by the browser to generate the HTML. If you make a request with CURL it will just show you the actual HTML content.You would need a Javascript engine to process the Javascript after receiving the response body.
just look at any web inspector tools (in chrome just ctrl+shift+i). here you can see the changes that the javascript has on the page reflected. I dont think curl or any curl-like-tool can do this.
This is a tough problem because the JavaScript has to run to get the right code. What I would say is download all the code locally and then add in an ajax call to the code, so it can ajax the source back to you after all the js has run. Then run the code in a browser.
If you need to do this a bunch of times you could queue these pages that needed to be loaded into a db and load all the pages using php. Then once the js has ajax'd the code back to the server it can refresh and pull the next page off the queue.
Let me know if you need me to clarify anything.
This can be done by headless browser activity like phantom js a great way to create your own logic whatever you want then get result array in console for php you can try activity here https://github.com/jonnnnyw/php-phantomjs & also https://github.com/ariya/phantomjs

How to load the entire (or more of the) DOM for inifinite scrolling page that uses Ajax?

If you look at a page like this: http://www.fieg.nl/ias-demo#/
You can see that it uses Ajax to dynamically add content when you scroll to the bottom, similar to how Google Images works.
If there was a page like this that I wanted to capture for parsing, I would do something like:
$page = file_get_contents("http://www.fieg.nl/ias-demo#/");
But this only gets everything that initially loads before any Ajax happens. Is it possible to use PHP or CURL or any other programs to capture the entire page and automatically load the Ajax content and capture that as well?
Also if there happens to be a weird page where it never stops loading things and literally goes on forever, I'm not sure how the tool would have to handle that. Because the tool would never find the end of the DOM in that situation.
Those pages work by sending an ajax request to fetch more data as the user scrolls towards the bottom of a page. The javascript then writes the response of the ajax request into the bottom of the page.
You need to run a tool like firebug to analyse the requests that are made to the server to retrieve the next page of content (using the net panel). Once you have found out the request URL, you need to emulate these requests in your PHP script.
Unfortunately SO is not the place for people to write your scripts to spider websites, but there is the theory anyway.

Delay for Simple HTML DOM Class

I'm using the simple html dom class and have gotten it to work on basic pages and can view the information I want. However, when I attempt to use it on a page that reloads a div with ajax, I can't seem to get it to "wait" before reading the page.
I basically want it to load the page, then wait 2 seconds before reading the page content (so that the new div has time to load). Is this possible or am I trying to use the class incorrectly? I'm manually inputting the URL, so it's not a link issue.
Example Page:
- You can see the load issue when you navigate through the pages.
Someone suggested curl and I tried that with the same results.
Thanks in advance.
PHP runs on the server. JavaScript (e.g. AJAX) runs in the browser, after the PHP code on the server has finished producing the page. You can't make a PHP program, running on the server, wait for an event that happens later in the browser.
You'll need to either load the content for that div using PHP code, or replace the PHP DOM-parsing code with JavaScript code that does the work on the client.
You can use the Sleep method ( http://php.net/manual/en/function.sleep.php ) if you simply want to delay the program execution for some set amount of time.

Trying to display a Loading screen in PHP, but doesn't display until after script is executed?

I am trying to display a loading message with an loading bar animation so people know the script is working away.
However even though the HTML is at the top of my PHP script , it only gets displayed once the script is fully loaded.
The script takes about 40 seconds to complete because it Generates a PDF, Inserts Data into a DB then sends a MIME email to an external MX (takes the longest part).
Here is the HTML that displays the loading message:
<div class="grid_16">
<fieldset>
<h2>
Processing <img src="img/loading.gif" alt="loading animation">
</h2>
</fieldset>
</div>
<?php
//rest of php page
Any idea how I display this out to the user so they don't get impatient and start hitting F5 etc.
Thanks
Merry Christmas!
PHP loads on the server and is then passed to the browser. This means that, until the page has completely finished loading on the server it will not be passed to the browser for display.
What you will need to do is use javascript and ajax. Firstly, remove the slow PHP from the HTML and show the page. Then once that has shown load the PHP script via ajax and once it has completed write it to the document.
My friend, PHP only sends output when it finishes script interpretation(1). So, your option is to put the gif in the preceding page fired (using some JavaScript) with the event that moves the user from oldpage.php to slownewpage.php. So, the gif will stay in front of user until PHP sends the new HTML result and the browser has something to work with.
(1) Unless you order him to do otherwise with something like ob_flush
run flush(); after the part you want to directly output (within php), because you are probably buffered especially if you are gzipping your code. You can load the page like others said and pull the result with javascript as well (actually that's probably the better option).
.. can I ask why you are sending a long message during a page load? Can't you have a cron job do it (check every few seconds to see if something needs to be sent)? I think they have cron programs for windows as well.

Stop or interrupt php script for particular time

Can I Stop or interrupt PHP script for particular time?
I would like to implement logic like below:
on mydomain.com/index.php there will be flash appear showing some intro of a product for suppose 20 sec. Once it complete, on same index.php page the home page of site will appear.
I am not aware about flash (action script).
Is it possible in PHP or Javascript ?
Usually "splash pages", as the're called, are made up of a seperate page.
In flash you can use the following code (Actionscript 3). Put it int the last frame, or use an event listener to redirecrect when the file is finished. The actual redirect looks like this:
getURL("http://www.woursecondpagehere.com", "_self")
Where you place it is up to you.
EDIT: I think that this is a reliable solution because this guarantees (if implemented correctly) that the page won't move until Flash is done. CSS and Javascript will work fine too.
There isn't a need to interrupt PHP in the scenario given. Though I think what you want is to load the rest of the HTML after a certain event occurs.
If thats the case then you can use AJAX to load the additional HTML from the server. Or you can use CSS to hide that content and show it after a certain point.
The META Refresh tag is probably not what you want since it will redirect the user after 20 seconds, regardless of how long it took to load your Flash file, then play it. Since the speed of the user's connection cannot be reliably predicted, you will probably end up with a poor user experience.
What you want to do is definitely possible but it will involve some interaction between the Flash object and the rest of your page. If you could do as Moshe suggested and simply have the Flash object redirect the user's browser to your actual home page with content on it, that would be easier.
If you insist on keeping everything on the same page, one way to do it is to call a Javascript function from the Flash object once it's finished playing. The function you call should be written to hide the Flash object and/or it's container and display the container () with all of your content that you're ready to show.
See this Codefidelity blog post for a quick tutorial on how to call JS functions from Flash.
Also, to clarify, you won't be interrupting or changing when your PHP script runs. That runs on the server before the page is created and sent back to the user's browser. All you need to do is structure the HTML/CSS of your page to have two DIVs: one with the Flash object in it and the other with all your normal page content. However, set the CSS to only show the DIV with the Flash object, then finally use Javascript to hide that DIV and show the one with the content in it.
Try this,
write the your flash (splash screen) <embede> code in index.html and simply use javascript redirect function with javascript timer functions to redirect to index.php where you actual content is there.
something like...
window.location = "http://your.domain.name/index.php"
or
location.href = "http://your.domain.name/index.php"
use setTimeout() to call redirect after specified time.

Categories