Refreshing an entire html/php page smoothly - php

Case: A php page with SQL needs to be refreshed every 30 seconds, but due to hardware limitations, there is a 0.5-2 second blank screen. Values do need to be retrieved from the database on every refresh, but the idea is to use a kind of seamless transitional effect, but where the transition is from one page to itself. 15 years ago or so, I experimented with what is described here: https://www.hongkiat.com/blog/meta-tag-hidden-features/ and I need something similar, though I am not even entirely sure that it would work. The page is very light and does not contain any images, so the load time is not the issue.
So, in short: How do I make it "impossible" to see that the page is refreshed, though values might have changed (or at least without the page going momentarily blank)?
I have tried numerous approaches with js and ajax functions to take care of the reloading instead of <meta http-equiv="refresh">, but none of them seem to be able to do what I want.

As suggested by #ivar and $usman-afzal, the issue was resolved using Ajax. Having no experience with Ajax/jQuery whatsoever, it took a while to get all the different values right and put them in their correct positions, but I succeeded, so thanks for the tip.

Related

Changes to code won't display from potential cache issue

There's an issue I'm experiencing that seems related to caching, but I'm not 100% positive.
I'm working with MAMP to render PHP inside of HTML. When I write some lines of code and go to the appropriate page, my code is manifest. However, if I make small changes to the code, I'm not able to get it to change with a refresh.
More than that, I'm finding that hard refreshes with and without cache clearing aren't solving the issue as well.
Here's a concrete example. Within PHP I declare a couple of numerical variables. I then set an if statement so that if the second variable is higher than the first, text is displayed on the page. When I change the value of the variables so the opposite would be true, the text continues to show on the page, even though the if statement is false.
I refresh, hard refresh, clear the cache, and even close the browser window. It often takes several combinations of these to finally register my change.
I thought the issue might have been with MAMP and so I tried it with XAMPP, but the results were the same. This makes sense, as they both use an Apache server (provided the issue is server related).
This is becoming very bothersome and I'm not sure where the issue lies. Any idea what might be the problem here?

Caching and session variables (PHP, MySQL...)

Apologies in advance for the lengthy post, just trying to explain the situation clearly.
I've created a PHP-driven website for searching a big (millions of records) MySQL database of people. On the search page you have your usual form for search criteria. Due to the way the people often use the site, the search criteria are saved into session variables so that if the search page is reloaded the previous criteria remain in the form fields (there's a button to manually reset the criteria, of course). This in itself works fine.
I also have two language selection links that store the language selection in a session variable (making the page header load an appropriate localization file), and as above, this in itself also works fine.
What's problematic is that when a user gets the search result, a list of people, and wants to open up detailed info on a person (thus going from search.php to info.php) and then wants to go back to the people listing via the back button, it takes too long to reload the previous page as the page re-sends the MySQL query etc, instead of going back to a cached page. It can take even 5 seconds or more sometimes as the queries produce up to 5000 results - but even say, 200-500 results takes long to reload because the database itself is big and not the fastest in the world. And limiting the number of results isn't really practical.
The obvious solution at first glance would SEEM to be enabling the browser cache. Which is exactly what I did via PHP header and pragma controls. And all seemed well, as going back to the list was basically instantaneous. However, I realized that enabling the cache means the updated session variables don't work. New search criteria doesn't properly replace the old ones when reloading the search page after having been to a different page, and even though you select another language, pages open up in the language you previously were using, because that's the way they were cached! You can force the language to update via F5, but that doesn't seem to help the search criteria much. But even if it did, F5-spam isn't really an answer, it needs to work automatically.
So long story short, how do I make the search result list open quickly without making session variables useless? Or will I simply have to make do with sluggish page loads when using back button, thus annoying users? I really don't want to open the info.php in a new page, either.
Have you considered caching the database results on the file system? I have found the Zend Framework caching class to be very easy to use. You can use any information you want, to differentiate cached results from other cached results. So the caching can be as fine grained as required.
http://framework.zend.com/manual/1.12/en/zend.cache.introduction.html
You don't need to use the whole of Zend Framework to use the class. You can use it on its own.

Why is my PHP Script not working when JavaScript is switched on? (but only in certain browsers)

I'm new to PHP (and web development in general) and have come across something that to me seems really, really bizarre!
Background
I am currently designing and developing an online enrolment form for my employer, a training company. The form consists of 3 pages - pages 1 and 2 are for data input whilst page 3 is a summary page with only one input, a box to check that the customer agrees to our T&Cs. Page 3 also includes a box where calculations are made as to the price of the selected training course based on choices made earlier in the form.
The form displayed on page 2, and some of the text on page 3, changes depending on a choice made on page one - that is, whether they are paying for the course themselves or their employer is funding it. If they choose employer, they get one form, if they choose self funding they get a different one. However, both of these forms are contained within a single .php file, using session variables to decide which one should be displayed.
I am using sessions to transfer the data between the pages. Each page has been made sticky using session variables. Real-time validation is carried out using javascript on each individual page, then a final PHP validation check is run on the whole thing when the customer tries to submit the final page. Javascript is also used on page one to calculate the price of the course in real time so the customer can see how much he/she will be paying before proceeding further.
The Problem
I find this really strange:
In Firefox, everything works perfectly
In Internet Explorer, when JavaScript is turned off, everything is fine. But when JS is switched on, the PHP validation on the final page seems to go haywire, thus making it impossible to submit the form.
In Chrome, again when JS is switched off, everything works, but when switched on, I can't even get past the first page. Instead of loading the correct form for page 2, I just get a blank screen.
Can anyone help? This is my first major project and I've been working on it for weeks, coming up against all sorts of problems and tearing my hair out but managing to solve them. Now I'm completely stumped, when I'm almost within touching distance of completion!!
I can't post the code here as it's 3 separate documents and very complicated, but if you want to have a look at the form itself go to:
http://testing.xenongroupadmin.com/testing/enrolment.php
I hope someone can help! Thanks very much!
You are most likely getting errors on the page2.php,
See error reporting to display and debug those:
http://ca.php.net/manual/en/function.error-reporting.php
When I have validation errors in non-Firefox browsers, it's usually because I have some error-reporting code in my JavaScript. Just something like
console.log(testValue);
can bring IE to its knees. If you have any code like that, remove it before testing in other browsers.
Also make sure that all variable declarations are done with "var", and that you're using semicolons at the end of each line (the spec should really enforce this, but I guess it doesn't, and browsers handle it differently).
I've only just seen all these answers, so apologies for not responding sooner.
I managed to fix the problem - turns out it was all to do with JS, no problems with my PHP at all. There were two issues. Firstly, a few syntax errors (which it took forever to find). Secondly, a lot of my HTML elements had id's and classes with identical names (e.g. id = "example" class = "example") which seemed to be throwing the javascript a bit.
Anyway, thanks for all the responses, and also apologies for not being very clear on some aspects - 'going haywire' was a pretty poor way of explaining the problem!!
Cheers

SQL Query seems to be executing twice [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Insert query on page load, inserts twice?
I'm hoping someone can help with a problem that's driving me up the wall. I've searched but found nothing directly answering this.
I have a problem on a website where an SQL INSERT query seems to run twice but only on some occasions.
It's a simple shopping site. On my product page there is an 'Add To Cart' button. On pressing this a user is taken to the Cart page, and various variable are passed across. An INSERT query is run when the page loads and the results are displayed in the shopping cart.
This all works fine most of the time, but on some occasions, the product is added to the database twice.
The problem is not related to any specific product. Since it works OK the majority of the time, I'm pretty confident the code is OK. The only thing I can say is that I have not as yet seen the problem outside of Firefox. Some people have suggested turning of the 'Disable Cache' via Toolbar, which I have, but still getting problems.
Very grateful for any suggestions.
What happens when you reload the cart page? Does the POST operation happen again - it could be that if there's a hang while the page is loading (which can happen with Firefox - particularly on Macs - something to do with its caching system) the user might be hitting reload and therefore resending the page data and running in INSERT query again.
IF this is the case...
... the best idea is probably to add in a step that deals with the database that outputs nothing (no output means no caching). So your "add to basket" form posts the data to a database handling script which performs the operations and then does a simple header("location:...") to pass the user off to the "display cart" page.
That way the script that performs the database operations displays nothing and is never cached - and the user can hit reload on the "display cart" page as often as they like and it'll just reload their existing cart.
Firstly, though it's not going to sound very helpful, the problem will be with your code, not with Firefox, PHP, or MySQL. I've been through exactly what you're going through, and been sure it must be a bug, but something this obvious would have been fixed by now.
To help you narrow down where the bug is occurring, it would be worth logging various points in your code, so you can see how it flows through. It may help to log the SQL queries as well, so you can see if they are being called in quick succession, or whether it is a loop of some sort where something else occurs before the second call.
As the insert is done in PHP, this is all carried out server-side, so the chances of it being browser-specific are pretty slim.
There's a lot of help on this site, but to give you any more concrete advice you'll need to post code, and a bit more detail about the flow of the application.
Good luck!

How to pass javascript based capability tests to server, and have server send markup based on test results

I'm considering doing capability/feature tests with JS and sending the results back to the server so it knows what it can/cannot send to client.
It's the same idea as modernizr server - https://github.com/jamesgpearce/modernizr-server
The problem I'm running into is communicating the JavaScript results back to the server on initial page load. I'm not sure what options are available but I know there are not a lot. I've tested setting a cookie and instantly reloading the page so PHP can read the results yet I'm concerned from an SEO standpoint. It just seems like instantly reloading a page will have some adverse affects, also I'm particularly worried if the refresh is on a page that has a form. Once the cookie is set and the user goes to another page, it works fine, it's just figuring out how to serve the content on the initial page load based on the capability tests. I've had a few different thoughts like just using JS to serve the markup on the initial page load and let PHP read the cookie on subsequent page loads, but I'm just not sure what might be the best solution.
I'm just at a loss as to what other options there are. I don't know which direction I should be looking in or if there is any direction at all. I don't know if AJAX would be able to help with this or not. I feel like I'm close, but figured maybe if I asked the community someone might have a good solution.
Thanks!
modernizr-server uses the method you described, it sets a cookie and then reloads the page. (Actually, it doesn't do any content output, the only thing on the initial page load is the JavaScript itself. Has an exit; statement if the cookie it's looking for isn't found.) Anyone who has JavaScript off could probably expect a blank page with it.
It looks like you have a couple of options (this is non-exclusive, there are more.):
Set a cookie and then reload.
Set a cookie, and then use AJAX to fetch the initial page's content. (As you mentioned)
Set an expected baseline of support (perhaps, expect no JavaScript support whatsoever), and serve that on your initial page load. If they have JavaScript on, you can either reload, or use AJAX to tell your server what things it supports and then reload chunks (or all) of the initial page.
Serve no-javascript content to just search engines, use option 1 or 2 for everyone else.
Option three here is the most work intensive, but probably the most inclusive option. (Edit: 3 and 4 will make sure search engines see your content)

Categories