SQLITE php javascript? auto refresh without entire page refresh - php

I have a simple php code which prints data from a SQLite database.
Basically $query ="Select A from B".
This all works fine. & when the sqlite db updates, i can refresh the page & the new data displays.
Want i am aiming to achieve is to refresh this data automatically every 5-10 seconds without having to reload the entire page.
I am also trying to avoid using iframes as there is about 20 of these on the page all displaying different data.
This has been driving me mad the last few days, Does anyone know of a way to do so?
My thought have been javascript, jquery or AJAX?
Other than that can you get sqlite data with javascript alone, without php?
& then implement something like the below on the element only? without the page reloading??
setTimeout("location.reload();",5000);
Thank you in advance.

You can do it using setInterval(), passing a jQuery ajax command and the time.
Or take a look at this jQuery plugin http://plugins.jquery.com/project/ekko

I'd recommend using jQuery as it's easy to do this sort of thing with the built in ajax function. Download jQuery and embed in your page or embed via a CDN.
1.) create a PHP file that outputs the content you want to "update" every few minutes. No headers/footers, etc.
2.) Put the content you want to refresh in a div with some specific id.
3.) Check out the simple example for using setInterval and ajax/load on this page.

Ajax is what you need.
Two options that I use quite a bit, both with plusses and minuses:
Jquery, easy to learn, quick to deploy, and very configurable
XAJAX -- AJAX for PHP. It's not the best thing out there, but if you're scared of Javascript, this allows you to do AJAX from PHP Functions, which can be easier for PHP guys to understand.
In essence, output your code to a specific DIV, then use AJAX to update that div with data it queries from the DB.

Related

Slow php/mysql results - display loading image?

I have a website that is running php/MySQL to query and return results. The result set is over 40,000 and the query is getting slower. I want to find a simple way to display a loading image while the page builds. I can do it with any page running JavaScript but I assume since php is server side I need to handle the loading differently.
I see that AJAX has a load() function where I can call the php page and presumable show a loading image while the request is loading. Is that the best way? I can't believe that there aren't lots of example scripts floating around since this has to be a very common problem.
Are you using an AJAX request to do this? If so, it's as simple as using JavaScript to display an image when you send the request, then hide it again upon success/error. You can just keep an <img> tag around with "display: none" and turn it on/off as needed.
If you aren't using AJAX, then this will be impossible, as the page does not exist until the query is completed.
On a side note: it's incorrect to say that "AJAX has a load() function". AJAX is not a code base or a library, it is a technique. Perhaps you mean jQuery.ajax, or some other library?
You got to use Ajax, i reckon Loading data from form into MySQL with PHP and AJAX will be useful and follow the jQuery API for Ajax reference

Add several specification without saving first

I'm working on a editing tool (type of a simple CMS) in PHP/MySQL for a product catalog. I have search the Internet for a solution but I don't even know what to search for. So now my hope is on you guys.
I have a form where you can put all kinds of data like part.no, description an so on. All of this data is saved into a MySql table (items). I also have a table with predefined specifications.
What I want to do, and that I can't find a solution for, is to have a dropdown meny (or similar) and a add button to add a row for each related specifications without saving the whole form each time. I want to save first when all specifications is selected.
So, can I use PHP for this or do I need jQuery/Javascript or similar? I know it's possible, have seen it in OpenCart :-)
I hope someone understands my question. It's hard to explane i a language I'm not fully manage.
Regards
Client-side vs Server-side
Javascript: This sits in the user's browser. So anything you want to move in the user's browser will be done with JavaScript. This is "client-side"
PHP: This site on the server, so takes inpute from the user's browser and gives back a response (generally HTML, but can also be JSON or XML which is read by Javascript.). This is "server-side".
Libraries
jQuery: This is a set of functions written for Javascript to make it easier. So it runs in the user's browser and makes it easier for you to write bits that move on the screen.
You get similar libraries that help you write PHP (commonly called "frameworks") and there are many others for javascript as well.
Where to start
Write your HTML page as you want it to look. Keep it simple for the first time.
Then write some javascript (possibly using jQuery) to move the menu. Google "jquery menu dropdown" or similar and you'll find a solution you cna customise.
Then write some PHP that gives you the HTML you wrote in '1'.
Then decide what's going to happen when you click on a link in the HTML, and repeat the process (write HTML, incorporate Javascript to make it move, write PHP to give HTML)
Then work out which bits of the HTML are common or structured and should come from a database.
Without writing it for you (in which case you'll never learn) best to start one bit at a time and build as your knowledge grows. Bucket loads of examples on the web when youreach a particular problem you need to solve.
After comment "[how to] make it possible to select and add single/multiple specifications (from another table) without saving the whole form each time a specs is added":
Growing with AJAX
What you are asking is AJAX - this is where you get Javascript to talk to the server, and for javascript to move bits on the page based on the results. jQuery is probably the easiest (and probably has best documentation / examples for the ajax, as well as moving the DOM).
Basically: you have an "event" that you trap in JavaScript, example
/// Using jQuery to trap a button click
$().ready( function() {
$("#ButtonID").click( function(e) {
e.preventDefault();
alert('Button Clicked');
});
});
Then you build in an AJAX call inside that event (also check out get or post as the syntax is easier, you just get less control). The AJAX wil send a request to your PHP server, and you can get PHP to return HTML which you can replace/insert using the DOM manipulation functions linked below (e.g. before, html etc) or, when you get more advanced, you'll send back JSON which is a data structure you cna more easily manipulate in JavaScript to stipulate what actions are required.
As above, without actually writing it for you, the best place to start is to read the docs and have a go. Google "jquery AJAX PHP table example" or similar and you'll find an example somewhere.

javascript php array

i have a js array like this:
var myArray = [];
myArray[1] = 'test';
myArray[2] = 'test';
-i want to hide it from users to see it. how can i store just the array in a php script and call it?
right now i have the .js separate from my form. and i just call it. but anyone can easily view source and visit the url using the .js name
-another question i have is to hide a url values from the user. i have something like this:
www.test.ca/people.php?id=12
i want to hide the values. thanks
For the JS code, if the browser has to execute it, then the user can see it. Not much you can do.
If you want to carry values between pages and you don't want them to be seen, don't use a query string -- use PHP sessions instead.
All Javascript code is viewable from the client. There really is no way around this.
Even an AJAX call can be viewed via a good browser plugin.
Javascript is a client-side executed script, so you won't ever be able to hide it.
You can encrypt it, you can make it difficult to view it, but that's pretty useless.
Just put it in your sources, or if you want to hide it a little further, get the array with an AJAX call, and make the call show nothing when it's not called with AJAX (the array can still be revealed with developper browser plugins, or with being hacked adding extra headers.
Here's the PHP condition code : if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])
Don't try to make it harder then that, it will be a waste of time.
Think that the browser is a transparent box. Everything you want to hide, needs to sit on the server.
If you want to send data across multiple pages, you have two options -
Use PHP Sessions
Use hidden fields
I would recommend the second option, because PHP sessions have the same problem as using a global variable, i.e., you can't use the same key in the whole applications, it is harder to maintain a session etc.
You can't hide a JS code from the user, because the browser will certainly execute it.

How do you make a web page change without reloading the page?

What should I look into to accomplish this.
When you select an input field some text to the right shows up.
For example: https://twitter.com/signup
Anyway i need something like that works with PHP. What should I look in to?
And also How can you query the database and not have to reload the page to see result? For example i have seen on many sites registration you can check if the a username is used without the page reloading. Dont know how to explain better.
Thanks
Like Arkain said, they are making the text appear with JavaScript. PHP is server-side only, meaning it can't make any changes to the page once it has loaded.
You can however, call a PHP script dynamically (to check if a username is registered) using a technique called AJAX.
I'd look into Ajax using jQuery. I had done some things with ajax before trying jQuery but jQuery made it so easy that I found it enjoyable to keep implementing things using it.
You need to use JavaScript, look at this source for instance, for some beginner tutorials.
Learn JavaScript. You can start with jQuery (a pre-made javascript toolkit of functions that help you do many things without reloading the page).
Google for a jQuery Ajax tutorial.

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