run query automatically after a data inserted to MySQL database - javascript

I'm trying to make a web based tracking system with a GPS that can be controlled by sending SMS.
How my system work:
When user clicks a button to know the GPS location, the system send an SMS (with certain format) by SMS gateway to the GPS device. The GPS will reply with its coordinate by SMS too. After that, the location information of the GPS such as latitude, longitude, and etc will be taken from the SMS with a function and a trigger (I already make it and it works) and the data will be inserted automatically to a table named tb_history.
I want to display the newest location data that just inserted to the table with this query:
SELECT * FROM tb_history WHERE tb_history.`id_gps`='1' AND tb_history.`date`='2014-04-24' ORDER BY tb_history.`time` DESC LIMIT 1;
my question is, is there a way I can run this code automatically after a data just inserted to the tb_history? my teacher say that maybe I can use a javascript that always refresh the page and stop refreshing if there was a data inserted to tb_history???
I really need an example to understand it...
I forgot to mention that I made my system with PHP and javascript
sorry for the bad english and Thank you for those who gave me the answer...

Use mysql trigger for this purpose http://dev.mysql.com/doc/refman/5.1/de/create-trigger.html

From your edited question I would guess you are confusing the CGI-script on the server that you implement in PHP with the webpage, generated by the PHP-script on the server and, loaded by the client that should be using Javascript to get an update from the server when the SMS returns from your application on the telephone.
Mister Henson gave you keywords for two good google searches about how the Javascript can learn about when the returned SMS arrives.
Two hits on the google search lead to this site:
What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
How do I implement basic "Long Polling"?

Related

AJAX - How can I build a notification system, that is constantly getting updated, without slowing down my website too much?

I am a beginner to web development, and I am trying to do a notification system with AJAX and jQuery.
In my web application, I have a comment system where you can mention another user. After a comment mentioning a certain user has been written, a new entry on my notifications table will be added, containing the comment, the id of the user who commented and the id of the user(s) who will receive the comment. After the notification is stored in the database, I want the person that was mentioned to receive the notification.
To that effect, I decided to use AJAX. Using the setTimeout() method, I am sending an AJAX request to the database every 2 seconds, and with that, I can display the notifications visually to the user that is meant to receive them.
My only concern is that this will slow down the site once I connect it with a server.
So, I was looking for a way that would allow me to implement a notifications system without slowing the site too much, since the one that I am using currently doesn't seem very efficient.
I would appreciate any help.

Send form data to local workstation to initiate a Photoshop script with parameters from data

I have a large Photoshop script (written in Javascript) that accepts parameters from me and then generates unique software files and mockups to client specifications. What I would like to do is create a form on the company website for new clients to fill out and then when the form is submitted, have my Photoshop script execute automatically, thus eliminating the need for me to manually enter clients' custom parameters. What I am wondering is if it is at all possible to do this. I would need to somehow track the event as it goes to the server hosting the website and then relay that to my work station. Or, would I have to use my work station as the server (at least for the form)?
I am hitting a wall when it comes to online research so if you don't have an answer, but perhaps have a keyword I have yet to google, that would be appreciated too! Thank you for your time spent reading this.
Details on the "parameters:" The data that needs to be sent consists of Strings and Numbers only. While it is impersonal and does not need encrytion, it could (in some cases) be way to long to fit using the GET method.
Side note: My most current research has lead me to investigate JSON.
I am not sure if I understood your problem :) , but if your problem is to transfer information from server to local machine for processing you can try :
1) store the submitted form on server in file or db,
2) write another script on your local machine that will run every 10 minutes for example (you can do it via cron job in linux) which will grab the new inserted data from server and pass to your photoshop script or store in local machine.
Or you can do by nodejs, I mean
1) the script from local machine connects via socket to the server and listens for new events
2) after each new substitution from website the script receives data from socket and runs photoshop script with given parameters

php or javascript which should I use

What would be the best way to go about this should I use php & mysql? so confused on where to start, still learning php, javascript
You will need a combination of (probably) all 3:
Javascript and HTML
Responsible for managing the user interface
Allows user to send a request to the server. Request can be "I want to challenge B" or "I will accept/deny challenge from A"
Allows user to ask for update from the server, and shows server messages to the user such as "A has challenged you" or "B has accepted/denied your challenge"
Must have access only to public information; no info you want to hide from the user (such as other users' emails or links to other challenges) should be visible to Javascript/HTML
PHP
Responsible for handling users' requests and returning responses or sending updates
Manages registrations, account changes, payments, account deletions etc...
Interacts with the database (eg: MySQL) to store or retrieve data
Generates challenge links
Tracks users' performance
Handles all the business logic and private data: how users get ranked, how users performed in challenges, did user enter correct password,...
MySQL
Responsible for storing user data and challenge data
Returns results to PHP when scripts ask for data
Updates stored data at PHP's command (eg: update users' rankings)
you can use javascript or PHP it does not matter.Only thing you got to remember is if you want to save details to Database like mysql then you got to send details to PHP.
You can use javascript to make the client side checks and send the finalized details to PHP for further processing. This reduces the server side processing.
You can do the HTML form check in PHP also if you do not want to use javascript.
If you are a beginner go with PHP you can easily find solution related to MySql/PHP on the web.
Thanks
depending on the needs for example i will use both javascript and php for validation of the form because
javascript will work faster but its code can be changed
php is slow but its code is hidden and cannot be changed

Using a server to send/receive information between a mobile phone and web page

I am trying to set up a simple set up as follows:
Have a mobile app with a page consisting of 4 lines (4 html paragraph lines (I am using phonegap)).
I want to use a web page from which I will input the data for those 4 lines. This information is sent to a server and that server transfers this information to that app on that mobile phone. Now, those 4 lines on the mobile phone is filled with the new information.
Similarly user inputs information on another page consisting of 10 lines of li (list). This information is again sent to the server and to the web page where the information is displayed.
I can almost feel the "internet police guys" getting all hyped and ready to vote this question down. But please understand that I have been on this site and various forums desperate to find a tutorial to guide me to do this and not able to find.
I am trying to use ajax to perform this setup. Confused how I would be using the php file. Information such as password n username is going to go in that php file to connect to the server. But php is a server side script thus needs to sit at the public_html folder. How do I use the php file from my desktop? Write a separate javascript to access it?
It is the concept that is confusing me. I am familiar with html,js,php.
I would appreciate any guidance or maybe a link to a tutorial which would help me to do the concept I mentioned. Thanks for listening.
You will need to create an API using PHP. This API is uploaded to your server and is considered "RESTful". Google a tutorial for what fits your needs. You can set all sorts of rules in this API such as requiring any requests to have an ID or access token.
Since you are using PhoneGap, your HTML and JS files rest on the device, so you will need to allow permissions to your API from anywhere. For this you will have to speak to your host provider about unless you know how to configure it yourself (some providers restrict what you want to do by default as an extra security precaution against XSS attacks).
Next, you can either use jQuery, or you can write some AJAX calls by writing the JavaScript yourself.
The most efficient way for this to work is to send JSON objects to and from the API. You will include a "command" in the JSON when you are sending from your app. On the PHP side, you will retrieve this command and use the rest of the data included in your JSON object to process the request. Your API will need to encode a JSON object for return (such as a user's profile information).
Here is a basic PHP API tutorial to get you going that explains some of the features of a RESTful API: PHP API
Here is a simple AJAX function (you will probably want to make this much more modular): AJAX
As broad as your question is, it seems like the best/easiest thing for you to do will be for you to first create a PHP webpage that will access a SQL database to perform the record updating. Actually, this should serve all of your needs for your mobile users assuming you don't need push notifications for live data updates.
I am assuming, since you are using phone gap, that you are more comfortable with web languages. After you get the webpage fully operational, then you should start building your app based on that exact same SQL database. With mobile app development there are a lot more "what if's" (what if the phone rings, what if the app is running in the background, what if there is no cellular service, etc...)
It is always easier to start with what you know and build on that, rather than starting with a new development platform and troubleshooting as problems arise.

Implementing web chat, how do I get typing status?

Can someone illustrate how I can get typing status of the other party with JavaScript?
UPDATE
Can someone recommend a one-to-one open source chatting application, preferably written in PHP? I only found open source chatting rooms which are for chatting among all onliners, but I just need a one-to-one chatting.
Here are a list of PHP-based open-source instant messaging software.
Some of those might be relevant for you.
For example, if you had an text area #chat then you could use this code to attach the event:
document.getElementById('chat').addEventListener('keydown', FUNCTION HERE, false);
See http streaming and some ready solutions here: http://ajaxpatterns.org/HTTP_Streaming
this is how google talk does it. And there are ready php or c++ solutions
It was quie a discovery for me!
This is an update to reflect the significant change in the OP's question:
Google Chat and Facebook both use XMPP(jabber) servers, as do most companies I know of that have internal instant messaging.
The nice part about XMPP is that you get all of the "is typing" and other presence-based information without having to roll-your-own in javascript (bear in mind, you will still need to use javascript to pass XMPP requests back to the server, but XMPP has most of the features you'd need already built in).
Check out OpenFire. It's a great XMPP server, totally open source, and they have a web-based version of their Spark client that is pretty nice.
Or you could get a PHP library for XMPP (there are a few). But you'd still need to have the XMPP server running in the background for PHP to work with.
Here's a list of XMPP libraries for PHP from XMPP.org:
Eiffel
JAXL
Lightr
Missus
xmpphp
Or, if you want to keep things mostly browser-side, they also have a list of libraries for javascript:
dojox.xmpp
js.io
JSJaC
strophe.js
xmpp4gwt
xmpp4js
I made a small chat application a while ago, and the only way to do it is to frequently check for new entries in the chat database and fetch anything newer than the last displayed message. At the same time as all that, you can check to see if the user's input is empty. If it is, do nothing. If it isn't, enter a status code into the database beside that user's name. If anyone has that status in the database when you're fetching information about new messages and who is online, you should display the 'user is typing' message. I hope that makes sense...let me know if it isn't.
For User1: If you save the chat message on each key-press to the database, with a status: sent=false and update the last updated date.
For User2: you could pole periodically for the presence of a message where sent=false and use the last updated to update user is typing message. if the lastupdated date is more than a say ten seconds you could remove the message as that person may have stopped typing. This will allow User2 to see User1 typing, stopping and continueing again.
Ideally polling for this information will be part of an existing call to the database to reduce additional overhead.

Categories

Resources