Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Realtime PHP

Author
Message
General Reed
18
Years of Service
User Offline
Joined: 24th Feb 2006
Location:
Posted: 14th Nov 2008 00:36
Hi all,
Basicly atm im creating a very dynamic website using PHP. But i have a question. For registering atm, you have to click sign up, before it checks your inputs are valid. Some websites seem to do them in realtime, yet they are php. How is this done? Facebook is an example, in the signup if you get something wrong, it instantly (without refreshing the page) says so.

Cheers.

General Reed

CPU: AMD X2 6000+ 3.0ghz GFX: NVIDIA BFG Geforce 8800GTS 640MB OC-550mhz core RAM: 2048mb

bitJericho
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 14th Nov 2008 00:39
look into ajax.


It's not just for BYOND you know!
Mnemonix
21
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 14th Nov 2008 02:28
It isn't realtime, what it is actually doing is using a technique known as AJAX and is calling a script without updating the page and only updating specific elements on the page.

TheSturgeon(playing me at chess) : I will use my powers of the horse and pwnzor you.
Lazlazlaz 1
18
Years of Service
User Offline
Joined: 18th Sep 2005
Location: England
Posted: 14th Nov 2008 03:24
Use jQuery, it makes it all so much easier.

Quick tutorial.


Test.php (using jQuery)




This file starts off with the obvious html type declarations, and also includes the jquery javascript file. In this example it is using the one hosted on their website, but I suggest you download a copy and host it yourself for speed.

After including the jquery file you have your ajax code.

The form is pretty simple, a textbox and a submit button. When the user enters stuff in the textbox and clicks submit the ajax stuff is done.

The basic outline of the ajax is, check the document is ready and fully loaded.
Then wait for any form submit buttons to be pressed (you can make it choose specific buttons, see the jquery website for more).
If the textbox has had the word 'correct' put into it, update the text in the previously empty span with 'Validated...'
Then return true (this means the javascript alert box in the form action is created).
If the word in the textbox is anything other than 'correct' update the span with text 'Not valid!' and fade it out in 1000 milliseconds. (Because of this, the .show() function is appended onto the update span text calls to ensure the text is shown and not still faded out).
Finally return false so the alert does not popup.



That is just one example. You can also make it interact with a database via a seperate file. The user never sees this other file. The ajax code bit calls this file with a GET or POST request and with appended data if needed, the file then returns stuff from the database and the ajax can update your page as above.

I suggest googling tutorials and looking at www.jquery.com for details on all the functions.


If you want to see that file in action have a gander at www.emigrategame.com/iamboard/test.php (shameless plug: nothing on that site is really working, just some ideas, but have a look around if you want).

As with most people I jump around which projects I work on and which I drop for a while.
Currently I'm back working on Sioux, a Hollywood Western RTS.
General Reed
18
Years of Service
User Offline
Joined: 24th Feb 2006
Location:
Posted: 14th Nov 2008 04:16
Thanks guys. Thing is im trying to use just PHP and HTML. Every tutorial etc i come accros, has loads of awkward, unflexable peices of javascript in the middle.

CPU: AMD X2 6000+ 3.0ghz GFX: NVIDIA BFG Geforce 8800GTS 640MB OC-550mhz core RAM: 2048mb

Lazlazlaz 1
18
Years of Service
User Offline
Joined: 18th Sep 2005
Location: England
Posted: 14th Nov 2008 04:33
I know what you mean, but a lot of it is required for ajax/jquery stuff. I had to use a bunch of different tutorials and my own experiments to get what I wanted.

I can write up a fuller example for a registration form if you want, but not until tomorrow now.

As with most people I jump around which projects I work on and which I drop for a while.
Currently I'm back working on Sioux, a Hollywood Western RTS.
General Reed
18
Years of Service
User Offline
Joined: 24th Feb 2006
Location:
Posted: 14th Nov 2008 05:12
Ok thanks, that would be great. Ive looked through a lot of tutorials, and to be honest, there not very good. They just show code, with no real explination on how the hell it works.

Im creating a sort of social network site, but im just begining to wonder if ajax is worth the pain?

CPU: AMD X2 6000+ 3.0ghz GFX: NVIDIA BFG Geforce 8800GTS 640MB OC-550mhz core RAM: 2048mb

Jeku
Moderator
20
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 14th Nov 2008 07:18
Well, the only way to do what you're thinking is with Javascript, aside from something like Flash.


Lazlazlaz 1
18
Years of Service
User Offline
Joined: 18th Sep 2005
Location: England
Posted: 14th Nov 2008 17:41 Edited at: 14th Nov 2008 17:44
Right, so dynamic register thing with ajax/jquery


There are 2 files: register.php and register_verify.php

register.php holds the frontend stuff, what the user sees, and all the ajax code. register_verify.php holds the database queries and return data.

I'm gunna reference these by line numbers so I suggest you stick it into something other than notepad, I like Crimson Editor myself, but anything with numbering will do.

This code will check to see if the username and email the user has entered is available or not, and if the password and password confirm fields match, outputting the response to the user without having to refresh the page.

register.php



Lines 2-5, just start a session and setup the database connection.

Lines 7-41, is the usual verification and database queries that you get with registering. Nothing to do with ajax going on here.

Line 49, include the jquery script. Always remember this, often things don't work because I forget it or forget to close the script tag.

Lines 50-135, this is the Ajax/jQuery stuff and what we care about at the moment. Before you look at this you will need to look at

Lines 156-217 to get my naming convention for div & span id's. Stuff with somethingAjax is where ajax sends it response to the users entered data.


I'll now break down the ajax.

Line 52

This waits for the document to load. The beauty of jQuery is that without it this is actually a quite hard thing to do and maintain cross-browser support. But by using jQuery all you need is this one line.
This creates an unnamed function that all your ajax code has to be inside. Note that it opens both a normal bracket ( and a curly bracket {, make sure you close both at the end as on line 134 with });

Lines 55-58 are not strictly needed, but it makes it easier. They assign elements to a variable.

This assigns the element (in this case a span) with id 'registerUsernameAjax' to the variable of the same name (but can be anything you want).

Line 61 creates a function that runs whenever a key is released, i.e. when the user stops typing. This function is associated with the 'username' input text field. Note that the same syntax is used to reference to both spans and input fields. They are ways around this so you can have things with the same id, but I prefer it this way. Keeps things more obvious.

So if the user stops typing, lines 62-84 are run.

Line 63 just stops you from having to type out 'this' all the time, instead only 't'. A bit lazy, but useful.

Line 65 ensures the code only runs if the actual text in the field has changed since it last checked.

Line 67, 70 & 82 ensures it only runs at a minimum of once every 200 milliseconds, this stops it hogging everything.

Line 69 sets the html within the variable (which relates to the element) as an animated loading spinner image. The .html command

overwrites anything else inside the element. It doesn't append onto it.

Lines 71-81 is the actual ajax call. It is all contained in the special ajax function $.ajax({ ... }); Remember to put both types

of bracket around it, and in the right order.
The function holds various types of data.
The url: line points to where the ajax request is to be sent.
The data: line holds the data to send, and is written just like a GET request on any url, even if you want to send the data as a POST request. In this case it says what element is being looked at, here username, and what value it holds, t.value
The dataType: line says what type of data is going to be returned. There are different ways of doing this: xml, json, html, but I find json to be the simplest. Have a look into all and decide for yourself. The major problem with json is that you cannot send direct html as it parses the data and mangles html tags. But you can work around this.
The type: line can either be GET or POST.

The last bit is the what-to-do-if-it-works function. This function takes a variable that is the response from ajax call, you can call it anything, but I use 'response' as it makes it obvious.
You can put what you like in the success function, but in here I have made it create an img element (line 77), then update the

source of the image with the value of the response (line 78) then update the html in the registerUsernameAjax span with this image. The image source from the response is either a tick or a cross.



This functionality is pretty much copied for the rest of the input fields, with some changes for the password fields. Instead of calling for a response from an external file, it simply compares the password field with the password confirm field, and if they are the same output a tick else it outputs a cross.




register_verify.php

Lines 5 & 21 determine what input field is being looked at, as said by the POST data sent to it via the ajax call.
For the username field, the value in the username field is searched for in the user database, and if it exists outputs a cross, else output a tick.
The important lines are 12-13 and 17-18.

The first line encodes the text "ajax_no.gif" as json data type, then echos it. This is what the ajax call reads. The second line is there to ensure nothing else is read and that the file terminates once the return data is sent.


To see all this in action, go to www.emigrategame.com/iamboard/register.php
A username that is unavailable is 'Laz'



If you want any more clarification or help, just ask. Again, check out www.jquery.com for loads more stuff you can do with it.

Edit: edited to sort out formatting

As with most people I jump around which projects I work on and which I drop for a while.
Currently I'm back working on Sioux, a Hollywood Western RTS.
General Reed
18
Years of Service
User Offline
Joined: 24th Feb 2006
Location:
Posted: 14th Nov 2008 20:31
Wow thanks Lazlazlaz 1. I will take a look at that

CPU: AMD X2 6000+ 3.0ghz GFX: NVIDIA BFG Geforce 8800GTS 640MB OC-550mhz core RAM: 2048mb

Lazlazlaz 1
18
Years of Service
User Offline
Joined: 18th Sep 2005
Location: England
Posted: 15th Nov 2008 01:55
no problem mate

As with most people I jump around which projects I work on and which I drop for a while.
Currently I'm back working on Sioux, a Hollywood Western RTS.

Login to post a reply

Server time is: 2024-05-08 04:16:32
Your offset time is: 2024-05-08 04:16:32