Refresh a page after data in MySQL database changes - php

Good day all,
Basically what's I've got is a PHP based site tied to a MySQL database all on a local web server (nothing is being accessed from outside of the company). The index page displays an image resembling a bar chart. Employees of the company will be entering data periodically which will update the image that appears on the index page. The index page will be displayed on a couple different screens throughout the company and I need that index page to refresh after someone alters the data in the database.
I've been messing around with various AJAX solutions, but as I don't know much about AJAX I'm having trouble adapting something to work the way I need. Here's the way I've was thinking about:
-- on the index.php run a JavaScript function every minute or so that gets a response from dataChanged.php
-- dataChanged.php will query the database and get a timestamp from one of the tables.
-- the script on the index.php will then compare the timestamp to the last time the page was refreshed (or some variable that stores such information) and refresh if the data is new.
I'm somewhat proficient in PHP, but am very limited with JavaScript (and thus AJAX).
Can someone get me pointed in the right direction?
Thanks!

What you want it the standard javascript function setInterval
Have it execute a ajax call every now and then to get new data. Try to get a standars librabry that knows this stuff, probably jQuery
Depending on if the chart generation is time consuming or not I would go with different strategies. The preferred way would be to just generate the graph on each call, but if that is very time consuming I think your two-step solution works great.

Check out the link http://blog.codebusters.pl/en/auto-refresh-content-after-changes-in-database-ajax.
Here you can see how to refresh your page after change in db.

Related

reflect the list without refreshing the page in php

I am trying to show the list of online users in my application. Let me explain my requirement.
I have a Mysql DB table where list of username and their status mode(either 1 or 0) are storing. I have two php pages. One is for listing down all user's name and the status mode. Second page is for editing the mode of users from 1 to 0 and vice versa.
Now I open these two pages from different system. If I change the status of one user(edit page) from one system then automatically it will reflect to the another system, where the listing page is opened, with the updated record and obviously this should happen before refresh the listing page. The same like gtalk chat users.
I am not asking the code, but please help me how to proceed to resolve the issue. Obviously, cronjob is one of the solutions, please provide another solution.
Thanks in advance.
Well cronjobs are in fact not the thing you need.
With cronjobs you can schedule a task. What you want is client side refresh when new info is found. While cronjobs are server side and always on an interval.
What you need is polling or commet
The first, polling, you use your client side to execute a script every x seconds and look if there is new info (waste of resources in my opinion).
Commet, is now a days a better solution. But often hard to implement. I used pusher for this type of stuff. You can push messages to (all) clients connected and say there is new info. And then they will update or with the message comes also the new info
To achieve something like this, you should use JavaScript and Ajax in the clientside.
Give the XMLHttpRequest a try. To make it easier you could use something like jQuery.
On the serverside you could use json to transmit the data.
Read the data from the table and put it into an array, let's call it users, the keys are the names and the values are their mode(1 or 0).
Then use json_encode(ARRAY):
//Echo the results in json format
echo(json_encode($users));
Let's say, the users 'Frank', 'Susan' and 'George' are online and 'Isabell' and 'John' are offline. Then the script would result in an output similar to this:
{"Frank":1,"Susan":1,"George":1,"Isabell":0,"John":0}
Of course you need to put this and the loading into another php-script, maybe refresh.php.
And, to read the data from the script, add some JavaScript to your view page.
Use the XMLHttpRequest to request data from the script you just added.
Or, if you use jQuery, you can simply use $.getJSON("NameOfTheScriptYouJustWrote") which returns an already parsed object.
Then use the returned data to update the list of users. And refresh it every 5-20 seconds.
And keep in mind that this is not an efficient way at all and that this will not work well if there are many clients using your service.

Safely execute queries via AJAX on document ready

Greeting everybody,
My problem is like this: I have some custom made statistics on my internet site where I log informations on what users do ( google analytics - like ). Obviously I aggregate information a couple of months, but the Tables I store information have grown too large and have a negative impact on page loading. The flow is like this ( in index, so affect all pages ) :
1. Get the included files
2. Execute part of statistics queries
3. Effective page code
4. Execute the last part of statistics queries
To get rid of this problem I want to make those queries on <body onload="execQueries();"> or on document ready with javascript / AJAX.
How can I safely and securely make those queries using AJAX, so that cannot be abused by a client with good knowledge of javascript/ajax. Because if I simply make that JS function it can be accesed everytime by a user with firebug.
The solution I think about is including the use of $_SESSION where I mark in top of my index.php information about those queries ( id, info ) and in the script called by AJAX I check if that $_SESSION['query_info'] is set and I execute it reading all the info from there, and then I use unset($_SESSION['query_info']);. So, if the AJAX is called again, because tat specific $_SESSION['query_info'] does not exists, I do not do anything in my DB.
Do you think this is a secure solution or do you have other ideas? Anything viable is welcomed.
Thank you
Try putting your related javascript codes into Closures.

Creating a Database that can be updated live

This is very hard to explain but I'm going to try.
We run a motor shop that has a QC program. The program was coded in access97 and it's time for an upgrade, we have elected to try a PHP/MySQL approach to do this.
Right now the access software has several pages to the form and each box sends to the database live so when you type something in you don't have to hit a save button or next or anything and when you come back it's there.
Also the forms are driven by an auto-incremented job number that you can punch into a field at the top of the page and it query's the server and displays all the data in the form boxes so you can edit it.
I don't know how to even start this project. I got a working form and an insert.php page but I don't know how to go about the rest.
If I could get a pointer in the right direction that would be appreciated. Thanks!
You just want it to save automatically? You'll have to look into JavaScript, and more specifically AJAX. I recommend using the jQuery library. Basically, you're going to want to make an AJAX call every time your form field is modified, and that AJAX call will simply update one field in particular.
I understand you are likely very new to website design, so this might be complicated for you.
I would read through this W3Schools tutorial. After reading through that, I'd pay close attention to this tutorial.
Again, this is difficult for beginners. I'd recommend you continue to work at your script, and ask more specific questions here on StackOverflow as time goes on. Good luck!
I have created a simple example here:
HTML/JS:
shaquin.tk/experiments/ajax.html,
PHP: shaquin.tk/experiments/qc.txt.
Have a look at the source to see how it works (I also have some comments in my code), feel free to copy it and modify for your own needs.
To sum up how it works:
When text is typed into a text box, a list of changed elements is updated.
Every updateInterval milliseconds (default 1000), the list is checked. (This helps reduce traffic and lag.) If anything has changed, the PHP file is called to update the database, and the list is cleared.
If an element loses focus and it has changed (e.g. copy/paste), the PHP file is called.
The PHP file sanitizes the query, checks for a valid job number, and updates the database.
References:
AJAX XMLHttpRequest
setInterval
addEventListener
encodeURI
mysqli_connect
mysqli_query
mysqli_real_escape_string
You'll need to submit the data as an ajax request. That way the data can be sent and returned without the page needing to be reloaded to update the information.

Making a menu of tables in php

Ok what I am trying to make is a system that supports tickets. Tickets have all kinds of info on a specific job. How can I make a ticket menu with like links to tickets which contain all the info I need. For instance I click on ticket number 777 so it I have the php?id=777 in the url.
I need this page to constantly look for new tickets.
what you need to do is add a the correct ticket to an anchor
Ticket 777
Ticket 778
on the page.php you can access the ticket number using the $_GET variable
<?php
$ticket = $_GET['id'];
/// now you have the ticket number in the variable
?>
I need this page to constantly look for new tickets.
I'm not 100% sure what you're exactly looking for here, but there are two basic solutions here:
AJAX to update the content without reloading the page
Reload the page every few minutes and have PHP handle it
The first is better for things that are constantly updated, like Twitter posts, news, stock-tickers, etc, but it's a little overkill for something that updates relatively rarely (takes longer than 6 minutes or so).
The tickets should be stored in some kind of database and then read out and looped over to create the table. This would probably make more sense in a list format (stacked divs) instead of a table, but then again, I don't know the specifics.
For the simple PHP generation of links, #Ibu has a good example.
EDIT:
For more information about implementing AJAX, this page has a good example. I would recommend using a framework like jQuery or MooTools to handle the AJAX because there are some inconsistencies between browsers.
EDIT:
From the comments you made, it sounds like you are not very familiar with how PHP works.
PHP is just a templating language with some programming language features. It is best used to generate pages on the fly with dynamic content from a database.
When you try to request a .php page, you are actually telling the server to execute the code in that file. When all the code is finished, the resulting document is given to the requesting browser. The result should be valid HTML if done correctly.

Write in MySQL database when something happens?

I'm building a game with javascript where you can walk and pick up things, right now it works fine but it doesn't save the things you have picked up on my database so when you quit the game you lose your progress.
I need to save to my database every time the user walks into something (when a div has a certain position). Right now there is a big javascript IF that basically deletes the item from the map.
Is it possible to make the javascript run the PHP code that writes in the database? How could I do it?
Since Javascript is client-side code, you can't simply call PHP (server-side code) with it. What you can do is post to a page that runs PHP code, using AJAX for instance.
The way you would do it is simple.
Every time the player does an action, have your script make an ajax call to the server to save the location (or what ever you are saving). You can just add a function to the part of your code, so when it is called, it sends the request in the background and saves the value to your databse. The only thing if that you would have to split the ajax requests up, so you only do one every 5-10 seconds. If you dont do this, then you would basically DDoSing you server.
you need ajax
http://code.google.com/edu/ajax/tutorials/ajax-tutorial.html

Categories