Jquery + PHP, wrapped in Phonegap, how to keep users logged in? - php

I'm making a simple mobile web app where users should be able to login/authenticate to sign up for private events.
I love the way JQuery mobile looks and feels, but want PHP to do everything in the backend: Handle form processing, talk to the DB and so on.
I currently have no authentication, but want to add that.
Wrapping the current solution in Phonegap actually worked, but I suspect the authentication/session handling is going to crash with that.
My hope is that I can exchange data with a PHP script when:
- the form loads (to check for PHP user session etc), and
- a "submit" button is clicked.
I covered the submit button part, and it works. Data is sent to, and returned from my PHP processing script to the JQuery front-end.
For handling the submit event, for some reason this code worked:
$(document).ready(function(){
as opposed to an onclick thing which didn't work.
This baffles me, as I read that document.ready is an event that occurs when the document has completed loading.
Q1: Could this have something to do with me using Jquery mobile sections (
Q2: What would be the correct JQuery event/trigger to use prior to loading, to control form "context"? (based on data from backend PHP).
Q3a: Is it a viable solution to rely on PHP sessions in such a case?
Q4: Also considering that I'd like to Phonegap it...will a PHP session be able to "see" the Jquery-based, phonegap-wrapped app like a client session just like if it was someone using a web-browser?
4b. When does a Phonegap app user session "end"? (When does the user have to log in again...).
I know similar questions have been raised several times, but please consider I'm a gullible idiot that knows too little to understand which case is similar enough to mine. There are some concepts here that I need to grasp before I can decide what to investigate and ask about :-)
Here's to tolerance and understanding! Cheers!
Eivind

You can store mobile id and users id in database with flag logged_in when they first logged in. Then again if user open that app, you can check the mobile id with the record in database whether user is exists or not and if exists then whats his/her status.

Related

automatic write back if user leaves page/system halts

I've been designing a site that is used to collect data, but the person I'm designing for wants some form of redundancy just in case the window is closed or the system shuts down. Is there any way to take data that's been collected and write it to a MYSQL database if the user is disconnected for a certain amount of time, or if they shut the browser window/shut the system down without submitting the data?
The web is stateless and disconnected - so all data will (or rather: should be) persisted between page requests.
I assume you have a web-page generated by PHP that contains a lengthy data-entry form, and you want to save the data in that form in the event the user closes their browser window - the solution is to use a client-script that polls the server with the current data in the form, or at the very least hooks on to the window close event.
Actual implementation is a task left up to the OP.
This can't be done just with a pure HTML page - if the user doesn't submit the form, your server doesn't know what they've typed.
However, you could put some Javascript on the page that made an AJAX call every few seconds (or every few key-stokes or clicks). The idea would be for the JavaScript to invisibly submit the whole form to a PHP page which saved it into a sort of "holding area".
If the user then submitted the form, the holding area could be cleared out, but if they never did, then the data in the holding area would show you where they got to.
The most common techniques to partially prevent this szenario is that web apps work with a heartbeat-function which fires via javascript in a constant interval and sends a request to the server, p.e. to show that the user is still logged on - or, in your case, maybe to submit data already typed into form fields, too.
Think of it as an ajax-powered auto-save-function!
You have to add some javascript to your code for this, but the commonly used javascript libraries, like jquery or mootools, are well documented and offer alot of examples how to do something like this.

pop up to another user of same application in php

I am using a php application. Here many users can login at same time like a web site.
Here a want when a user is saving form , a pop up should automatically appear on the browser of another user. id of 2nd user will be mentioned in form , when 1st user is savinf the form.
Please any body give me any idea.At least what type of technique i should use and what type of technology is required.
Thanks !
Rakesh
The technology you need is AJAX.
You should write a javascript function that calls periodically your server (through XMLHttpRequest) for any event you want to notify to your user,and when you have a relevant answer, that same function should pop up the warning.
You'll probably want to start using jQuery, a wonderful Javascript library (that includes Ajax calls) that will save you a lot of time.

Fill Forms Offline

This is what I am trying to implement and I don't know how to do it:
I have a few forms that users should be able to complete offline. Once the user has filled in the form they should be able to hit save and their data needs to be saved offline. Then later when they are online they can submit all the form data which was saved by hitting a button. They will be asked to log in and once they have done so their data will be saved on server.
These forms will be submitted via iPad/iPhone. The user can save multiple copies of each form.
I am looking for an HTML5/JavaScript/jQuery solution. My backend is php. I have not tried anything yet as I don't have any idea how to do this. Where do I start?
Thanks
You can provide a manifest file to provide a specific form to a user - Application cache IIRC:
http://dev.w3.org/html5/spec/offline.html
Caveat - without making it a native app the user would have to have visited the website while online before they will see any forms at all
Then once the user fills this in store the results in localStorage, ready to access when online.
If the user is online, the standard page will be displayed, and you can check local Storage for any unsubmitted forms, and on submission remove from localSorage.
Check out the principles used in this tutorial, they should get you most of the way there, but you'll have to look into offline html and localStorage a lot more to get a working app:
http://sixrevisions.com/web-development/html5-iphone-app/
Hope that helps.
I don't think this is possible as an web-only solution targeting iOS, since you can't count on the forms staying up in Safari. Even if the user loaded the form in their browser ahead of time when they had an active internet connection, you can't count on Safari not trying to reload the page (which would clear the page and give a connection failure since they're offline).
Therefore, I think you have to create a native iOS app for this functionality.
If you were targeting desktops instead, you could probably accomplish this with browser plugins.

AJAX Some questions

So I am very new to this concept.
So why not go headfirst :) Some things I don't understand;
What happens if js is disabled?
If using mysql databases (ie; checking forms and such) why not just use php?
To confirm what others have said, disabling Javascript will also disable the AJAX call. After all, AJAX stands for "Asynchronous Javascript and XML".
To address why you can't just use PHP, there are some things that just can't be done without it. PHP is great to load the page with the initial information, but after the page is loaded, it actually requires the page to be reloaded to load something else. AJAX allows you to get around this hassle.
For your example of form validation, AJAX can be used to validate the information while the person is filling it out. Otherwise, you are required to reload the page each time someone fills out another field in the form.
Another example is from a project that I have worked on. The form required a zip code and would load the appropriate city and county based on the inputted zip. Using strict PHP, I would need the client to download the entire zip table embedded in the HTML/JS (which would add another 100k at least to the download).
Using AJAX, I can get around this. The user can input the zip code, which triggers an AJAX call that downloads the few rows that I need (this will be less than a few hundred bytes, for comparison).
[Edit:] Also, a tip because you said that you were new to AJAX. If your dealing with some form of authentication (logging in, etc.), remember to validate the user on the AJAX pages themselves. Otherwise, tricky users will be able to access sensitive information for your database.
Ajax just adds to the user experience and allows a web application to feel more like a desktop application to users. So they can delete a record and stay on the same page without reloading, you just let the record disappear.
And remember to validate on the server-side, even if you validate on client-side. Your weakest at your client-side as someone can easily just submit the values straight to your script so ALWAYS check on the server-side and do client-side if you would like to add some nice effects etc.
But you will always need to keep in mind that there are people out there who have javascript disable be it a security policy or just because their paranoid. So when you don't have JS enabled you javascript and AJAX requests won't work. So while developing you will need to make sure that if javascript is not their to do the operation that the form is submitted just like a normal HTTP form, this will allow all those paranoid people to also use your application :D.
OR you could always just deny access to those who don't have Javascript enabled but that's not very nice ... So if you want to check if they have javascript enabled checkout - http://www.w3schools.com/TAGS/tag_noscript.asp - for a example.
AJAX is a Javascript client based technology. If js is disable it simply doesn't work.
Php is a server based technology.
In Php you write pages that are dinamically built by the server. Once built they are sent as html to the client.
Using javascript (and Ajax) you can call the server just to request some datas (hint: look at JSON) or just a little html snippet which is plugged in the current page directly by the browser without requesting a full refresh from the server.
With js and AJAX you can achieve a very rich client experience without reloading a full page every time.
I believe nothing will happen if js is disabled. You need js to grab the data.
If you want to use mysql databases, you can use js to access a php script, which can then return any data gathered from a database, rather than doing it in the page.
AJAX is a way for Javascript (client side) to access PHP/ASP/Whatever serverside language you are using. This means, that if you have an PHP script for getting some data from your MySQL database, and want to run that script when the user clicks some random button, AJAX can do that (async)m and you wont have to reload you page to execute the PHP script.
If Javascript is diabled, AJAX won't work.

Record User Actions Using PHP and Javascript

I am developing an online learning system (PHP, MySQL and Javascript). I would like to track what pages and how long each users spent on each page. Ideally, I would like to record this in a MySQL database. My question is 2 fold:
1. What kind of fields would I include in my db table to record multiple pages accessed?
2. Is this problem best approached by server side only or by using javascript ? e.g. server side: hidden form fields with a page id attached, page id is passed to db and recorded?or Javascript: record all actions in Javascript variables and somehow pass to db at end of session?
Really I am just looking for some high level guidance on an approach as opposed to code snippets.
GF
PHP isn't my normal language, but I would think about creating a module of code that can be called from the top of each of your scripts, that basically logs away "I served this page, with these form variables, at xxx ... ". To be more precise, I would record that in a table.
If you need to know when the user left your page, for a page on another site or perhaps shut down their browser for instance, then a purely server side solution isn't going to cut the mustard. In that event, you are going to have to start thinking about JavaScript, and intercepting events - such as the onUnload event...
Have a read here...
While I know of no solution that can track individual users out of the box (I'm sure there are some), I am pretty sure you could customize Piwik to do this. Piwik aims to be a self-hostable alternative to Google Analytics. It is open source and build on Zend Framework and MySql.
Piwik collects usage statistics through a JavaScript tracking code and a Webbug image for fallback. Basically, what you would need to do is pass the logged in user's user id to the tracking script and then write a plugin that knows how to handle this information.

Categories